merge objects javascript es6 code example
Example 1: merge two objects javascript
const object1 = {
name: 'Flavio'
}
const object2 = {
age: 35
}
const object3 = {...object1, ...object2 } //{name: "Flavio", age: 35}
Example 2: javascript merge objects
var person={"name":"Billy","age":34};
var clothing={"shoes":"nike","shirt":"long sleeve"};
var personWithClothes= Object.assign(person, clothing);//merge the two object
Example 3: 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 }; // or { ...response } if you want to clone response as well
console.log(newItem );
Example 4: how to merge two objects into one in javascript
let obj1 = { foo: 'bar', x: 42 };
let obj2 = { foo: 'baz', y: 13 };
let clonedObj = { ...obj1 };
// Object { foo: "bar", x: 42 }
let mergedObj = { ...obj1, ...obj2 };
// Object { foo: "baz", x: 42, y: 13 }