Combine data of object/array to an attribute v2
JavaScript
let foo = [];
foo[0] = [];
foo[1] = [];
foo[2] = [];
foo[0].id = "45g-332";
foo[0].id2 = "45G-000332";
foo[0].description = "tomatoes";
foo[1].id = "45f-842";
foo[1].id2 = "45F-000842";
foo[1].description = "cherries";
foo[2].id = "45g-332";
foo[2].id2 = "45G-000332";
foo[2].description = "peaches";
let bar = [];
for (let i in foo) { // Loop through source (foo)
if (bar[foo[i].id]) { // Check if sink (bar) with id already exists
bar[foo[i].id].description += "; " + foo[i].description; // Add description to existing bar element
} else {
bar[foo[i].id] = []; // Create new bar element
bar[foo[i].id].description = foo[i].description;
};
bar[foo[i].id].id2 = foo[i].id2;
};
for (let i in bar) {
console.log("id: " + i + " has: " + bar[i].description + " and id2: " + bar[i].id2);
};