1const object1 = {
2 name: 'Flavio'
3}
4
5const object2 = {
6 age: 35
7}
8const object3 = {...object1, ...object2 } //{name: "Flavio", age: 35}
9
1var person={"name":"Billy","age":34};
2var clothing={"shoes":"nike","shirt":"long sleeve"};
3
4var personWithClothes= Object.assign(person, clothing);//merge the two object
5
1let obj1 = { foo: 'bar', x: 42 };
2let obj2 = { foo: 'baz', y: 13 };
3
4let clonedObj = { ...obj1 };
5// Object { foo: "bar", x: 42 }
6
7let mergedObj = { ...obj1, ...obj2 };
8// Object { foo: "baz", x: 42, y: 13 }
9
10
1const response = {
2 lat: -51.3303,
3 lng: 0.39440
4}
5
6const item = {
7 id: 'qwenhee-9763ae-lenfya',
8 address: '14-22 Elder St, London, E1 6BT, UK'
9}
10
11const newItem = { ...item, location: response }; // or { ...response } if you want to clone response as well
12
13console.log(newItem );
1// reusable function to merge two or more objects
2function mergeObj(...arr){
3 return arr.reduce((acc, val) => {
4 return { ...acc, ...val };
5 }, {});
6}
7
8// test below
9
10const human = { name: "John", age: 37 };
11
12const traits = { age: 29, hobby: "Programming computers" };
13
14const attribute = { age: 40, nationality: "Belgian" };
15
16const person = mergeObj(human, traits, attribute);
17console.log(person);
18// { name: "John", age: 40, hobby: "Programming computers", nationality: "Belgian" }
19