javascript merge objects 1

javascript merge objects

var student = {name: "Rahul", age: "16", hobby: "football"};

//using ES6
var studentCopy1 = Object.assign({}, student);
//using spread syntax
var studentCopy2 = {...student}; 
//Fast cloning with data loss
var studentCopy3 = JSON.parse(JSON.stringify(student));

Here is what the above code is Doing:
1. We have a student object with three properties.
2. We are creating a copy of the student object using Object.assign() method.
3. We are creating a copy of the student object using spread syntax.
4. We are creating a copy of the student object using JSON.parse() and JSON.stringify() methods.

Similar Posts