Answers for "copy objects in javascript using object destructuring"

15

clone object in js

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));
Posted by: Guest on April-29-2020
0

javascript clone object

// syntax: let <newObjectName> = {...<initialObjectName>};

// example: 
const me = {
  name: 'Jakes',
  age: 30,
};
const friend = {...me};
friend.age = 27;
console.log(friend.age); // 27
console.log(me.age); // 30

// -----------------------------
//  BAD
// -----------------------------
const me = {
  name: 'Jonas',
  age: 30,
};
const friend = me;
friend.age = 27;
console.log(friend.age); // 27
console.log(me.age); // 30
Posted by: Guest on December-08-2020

Code answers related to "copy objects in javascript using object destructuring"

Code answers related to "Javascript"

Browse Popular Code Answers by Language