I have two Object like this and want to merge them:
const obj1 = {
1: { foo: 1 },
2: { bar: 2, fooBar: 3 },
3: { fooBar: 3 },
};
const obj2 = {
1: { foo: 1, bar: 2 },
2: { bar: 2 },
};
const merged = someMergingMethod(obj1, obj2);
merged === {
1: { foo: 1, bar: 2 },
2: { bar: 2, fooBar: 3 },
3: { fooBar: 3 },
};
I mean, I want to not only merge the objects, but also merge their object values' properties too if the key is duplicated.
Because just merged = { ...obj1, ...obj2 };
overwrites the properties with obj2's.
What is the easiest way to do this?
I can use ES2017 and other libraries like lodash.