What is the best way to merge two object and return a combined one using javascript?
For Merging two different object you can do this
/**
* Merge Two objects and return combined object
*/
merge = (target, source) => {
// Iterate through `source` properties and if an `Object` set property to merge of `target` and `source` properties
for (const key of Object.keys(source)) {
if (source[key] instanceof Object) {
if (target[key]) {
Object.assign(source[key], this.merge(target[key], source[key]));
}
}
}
// Join `target` and modified `source`
return Object.assign({}, target || {}, source);
};
pre
& code
tags ):