combine 2 objects js code example
Example 1: merge two objects javascript
const object1 = {
name: 'Flavio'
}
const object2 = {
age: 35
}
const object3 = {...object1, ...object2 }
Example 2: merge objects js
Object.assign(obj1, obj2);
const allRules = Object.assign({}, obj1, obj2, obj3, etc);
Example 3: javascript merge objects
function mergeObj(...arr){
return arr.reduce((acc, val) => {
return { ...acc, ...val };
}, {});
}
const human = { name: "John", age: 37 };
const traits = { age: 29, hobby: "Programming computers" };
const attribute = { age: 40, nationality: "Belgian" };
const person = mergeObj(human, traits, attribute);
console.log(person);
Example 4: two object combine together javascript
const a = { b: 1, c: 2 };
const d = { e: 1, f: 2 };
const ad = { ...a, ...d };
let objs = [{firstName: "Steven"}, {lastName: "Hancock"}, {score: 85}];
let obj = objs.reduce(function(acc, val) {
return Object.assign(acc, val);
},{});