1const object1 = {
2 name: 'Flavio'
3}
4
5const object2 = {
6 age: 35
7}
8const object3 = {...object1, ...object2 } //{name: "Flavio", age: 35}
9
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 );
1const obj1 = {'a': 1, 'b': 2};
2const obj2 = {'c': 3};
3const obj3 = {'d': 4};
4
5const objCombined = {...obj1, ...obj2, ...obj3};
1// Exporting individual features
2export let name1, name2, …, nameN; // also var, const
3export let name1 = …, name2 = …, …, nameN; // also var, const
4export function functionName(){...}
5export class ClassName {...}
6
7// Export list
8export { name1, name2, …, nameN };
9
10// Renaming exports
11export { variable1 as name1, variable2 as name2, …, nameN };
12
13// Exporting destructured assignments with renaming
14export const { name1, name2: bar } = o;
15
16// Default exports
17export default expression;
18export default function (…) { … } // also class, function*
19export default function name1(…) { … } // also class, function*
20export { name1 as default, … };
21
22// Aggregating modules
23export * from …; // does not set the default export
24export * as name1 from …;
25export { name1, name2, …, nameN } from …;
26export { import1 as name1, import2 as name2, …, nameN } from …;
27export { default } from …;
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