How to copy all properties from one source objects to a target object

Posted By : Priya verma | 30-Dec-2016

The Object.assign() method is used to copy the values of source objects to target object without any referencing.

Syntax:

Object.assign(target, ...sources)

Parameters

Target: The target object.

Sources: The source object(s).

Return value : The target object.

 

Example :

let obj1 = { a: 1 };

let  obj2 = Object.assign({}, obj);

console.log(obj2); // Output :{ a :1}

// Merge an object

 

let first = {name: '/abc'};

let last = {lastName: '1'};

let person = Object.assign(first, last);

console.log(person);  // {name: 'abc’, lastName: '1'}

THANKS

About Author

Author Image
Priya verma

Priya is a bright UI designer, having skills in , HTML, CSS, javascript, bootstrap. Her hobbies are playing outdoor games.

Request for Proposal

Name is required

Comment is required

Sending message..