Alright, so this is what I have in mind.
I have two example objects
let obj1 = { x: 60 };
let obj2 = { x: 9 };
I want to merge these objects in a way that their integer properties will combine as well, and not override each other, so the end result will be
let obj3 = { x: 69 };
So I've looked into Object.assign, but this function only merges properties in a way that it favors the properties on the first object if the other objects have a property with the same name, and doesn't sum integer properties.
I can, of course, just make a function that loops over each object's properties and creates a new object with the summed properties, but that'd be longer and I wanted to know if there's already a function that does it easily like the way Object.assign acts.
Thank you.
If you want to use Lodash
library you could do this with mergeWith
method.
let obj1 = { x: 60, b: "foo", c: {y: 3, x: 'bar'} };
let obj2 = { x: 9, a: 'bar', c: {y: 4} };
const sum = _.mergeWith(obj1, obj2, (a, b) => {
if (_.every([a, b], _.isNumber)) return a + b;
})
console.log(sum)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>
You could reduce the objects by taking an arbitrary count of objects and reduce the key/value pairs by respecting nested objects.
const
add = (...a) => a // take all parameters
.map(Object.entries) // get entries
.reduce((a, b) => [...a, ...b], []) // flat entries
.reduce((o, [k, v]) => {
o[k] = v && typeof v === 'object' // assign if object
? add(o[k] || {}, v) // result of recursive call
: (o[k] || 0) + v; // or sum the value
return o;
}, {});
let obj1 = { x: 60, y: { z: 3 } },
obj2 = { x: 9, y: { z: 1, a: 32 } },
obj3 = { y: { a: 10 } },
result = add(obj1, obj2, obj3);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With