how to merge two objects into one in javascript 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: 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 }

Example 3: javascript combine objects

const obj1 = {'a': 1, 'b': 2};
const obj2 = {'c': 3};
const obj3 = {'d': 4};

const objCombined = {...obj1, ...obj2, ...obj3};

Example 4: javascript export default

// Exporting individual features
export let name1, name2,, nameN; // also var, const
export let name1 =, name2 =,, nameN; // also var, const
export function functionName(){...}
export class ClassName {...}

// Export list
export { name1, name2,, nameN };

// Renaming exports
export { variable1 as name1, variable2 as name2,, nameN };

// Exporting destructured assignments with renaming
export const { name1, name2: bar } = o;

// Default exports
export default expression;
export default function () {} // also class, function*
export default function name1() {} // also class, function*
export { name1 as default,};

// Aggregating modules
export * from; // does not set the default export
export * as name1 from;
export { name1, name2,, nameN } from;
export { import1 as name1, import2 as name2,, nameN } from;
export { default } from;

Example 5: javascript export default

export default const array = [1, 2, 3];
export default let array = [1, 2, 3];
export default var array = [1, 2, 3];
export default const variable = 'value';
export default let variable = 'value';
export default var variable = 'value';
export default const variable;
export default let variable;
export default var variable;
export default function functionName () {
  // ...
}
export default class ClassName {
  // ...
}
// Before you use export default: You can't have 2 or more export default's.