Similar to this question asked previously, is there an easy way to sum two objects, or an array of objects, by key in Lodash?
{ a:12, b:8, c:17 }
and
{ a:2, b:3, c:1 }
should give
{ a:14, b:11, c:18 }
Similar to this question asked previously, is there an easy way to sum two objects, or an array of objects, by key in Lodash?
{ a:12, b:8, c:17 }
and
{ a:2, b:3, c:1 }
should give
{ a:14, b:11, c:18 }
You can use lodash's _.mergeWith()
:
var o1 = { a:12, b:8, c:17 };
var o2 = { a:2, b:3, c:1 };
var result = _.mergeWith({}, o1, o2, function(objValue, srcValue) {
return _.isNumber(objValue) ? objValue + srcValue : srcValue;
});
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>
If you know that all values are numbers, or you always want to add values together, you can use _.add()
as the merging customizer:
var o1 = { a:12, b:8, c:17, d: 'a' };
var o2 = { a:2, b:3, c:1, d: 'b' };
var result = _.mergeWith({}, o1, o2, _.add);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>
To merge an array of objects use spread syntax with _.mergeWith()
:
var arr = [{ a:12, b:8, c:17 }, { a:2, b:3, c:1 }];
var result = _.mergeWith({}, ...arr, function(objValue, srcValue) {
return _.isNumber(objValue) ? objValue + srcValue : srcValue;
});
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>
Or with Array.reduce()
:
var arr = [{ a:12, b:8, c:17 }, { a:2, b:3, c:1 }];
var result = arr.reduce(function(r, o) {
return _.mergeWith(r, o, function(objValue, srcValue) {
return _.isNumber(objValue) ? objValue + srcValue : srcValue;
});
}, {});
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>
One solution is to use assignWith and add
var o1 = { a:12, b:8, c:17 };
var o2 = { a:2, b:3, c:1 };
const combined = _.assignWith({}, o1, o2, _.add)
console.log(combined);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>