Unique Array Of Objects And Merge Duplications Using Lodash Or Underscore
I have an array of objects that can contain duplicates, but I'd like uniquify the array by a property, and then I want to merge properties of the duplicates conditionally. For exam
Solution 1:
You could do it using _.uniqWith
, but it would require you to mutate one of the parameters before returning the boolean value of whether they are equal, so I don't recommend it.
A better option is to use _.groupBy
followed by _.map
in order to group the unique names, and then customize how you combine them. In your case, you can combine the same-named objects using _.assignWith
and then give a logical or ||
operation as the combining function.
With ES6, it becomes nearly a one-liner:
var array = [{"name":"object1","propertyA":true,"propertyB":false,"propertyC":false},{"name":"object2","propertyA":false,"propertyB":true,"propertyC":false},{"name":"object1","propertyA":false,"propertyB":true,"propertyC":false}];
var updatedArray =
_(array)
.groupBy('name')
.map(objs => _.assignWith({}, ...objs, (val1, val2) => val1 || val2))
.value();
console.log(updatedArray);
.as-console-wrapper { max-height: 100%!important; top: 0; }
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Solution 2:
With plain Javascript, you could use a hash table as reference to unique objects with the same name and collect the values in a loop over the keys and update with logical OR.
var array = [{ name: 'object1', propertyA: true, propertyB: false, propertyC: false }, { name: 'object2', propertyA: false, propertyB: true, propertyC: false }, { name: 'object1', propertyA: false, propertyB: true, propertyC: false }],
unique = array.reduce(function (hash) {
returnfunction (r, a) {
if (!hash[a.name]) {
hash[a.name] = { name: a.name };
r.push(hash[a.name]);
}
Object.keys(a).forEach(function (k) {
if (k !== 'name') {
hash[a.name][k] = hash[a.name][k] || a[k];
}
});
return r;
};
}(Object.create(null)), []);
console.log(unique);
.as-console-wrapper { max-height: 100%!important; top: 0; }
Post a Comment for "Unique Array Of Objects And Merge Duplications Using Lodash Or Underscore"