merge function javascript code example
Example 1: merge objects js
Object.assign(obj1, obj2);
const allRules = Object.assign({}, obj1, obj2, obj3, etc);
Example 2: array merge in javascript
function mergeArrays(arr1, arr2) {
return Array.from(new Set(arr1.concat(arr2).sort((a,b) => (a-b))));
}
Example 3: react merge two objects
const object1 = {
name: 'Flavio'
}
const object2 = {
age: 35
}
const object3 = {...object1, ...object2 }
Example 4: javascript object shallow merge
var obj1 = {
foo: {
prop1: 42,
},
};
var obj2 = {
foo: {
prop2: 21,
},
bar: {
prop3: 10,
},
};
var result = {
foo: {
prop2: 21,
},
bar: {
prop3: 10,
},
}