There are quite a few npm packages out there to accomplish this, but one that is very popular is lodash.merge
.
Take a look at the lodash merge function:
https://lodash.com/docs/4.17.4#merge
This method is like _.assign except that it recursively merges own and
inherited enumerable string keyed properties of source objects into
the destination object. Source properties that resolve to undefined
are skipped if a destination value exists. Array and plain object
properties are merged recursively. Other objects and value types are
overridden by assignment. Source objects are applied from left to
right. Subsequent sources overwrite property assignments of previous
sources.
And an example:
var object = {
'a': [{ 'b': 2 }, { 'd': 4 }]
};
var other = {
'a': [{ 'c': 3 }, { 'e': 5 }]
};
_.merge(object, other);
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }
Use the lodash.merge
npm package to pull in just this method:
https://www.npmjs.com/package/lodash.merge
Good luck!
1
solved How do you merge two javascript objects [duplicate]