I have an array like so which i am trying to merge so any object that has the name property the same will after the merge contain a list of merged objects
va
Another approach using Lodash with just chain and reduce
var array = [
{ name: "One", myList: ["Object1", "Object2"] },
{ name: "Two", myList: ["Object3", "Object4"] },
{ name: "One", myList: ["Object5", "Object6"] }
];
const newArray = _.chain(array)
.reduce((acc, currentValue) => {
acc[currentValue.name] = (acc[currentValue.name] || []).concat(
currentValue.myList
);
return acc;
}, {})
.reduce((acc, currentValue, key) => {
acc.push({ name: key, myList: currentValue });
return acc;
}, [])
.value();
console.log(newArray);