es6 merge object code example
Example 1: javascript merge objects
var person={"name":"Billy","age":34};
var clothing={"shoes":"nike","shirt":"long sleeve"};
var personWithClothes= Object.assign(person, clothing);
Example 2: merge objects javascript es6
const response = {
lat: -51.3303,
lng: 0.39440
}
const item = {
id: 'qwenhee-9763ae-lenfya',
address: '14-22 Elder St, London, E1 6BT, UK'
}
const newItem = { ...item, location: response };
console.log(newItem );
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);