On gists
JS objects copies, shallow, deep, lodash etc ...
JavaScript
copies.js
Raw
#
/*
All ways
===========
https://code.tutsplus.com/articles/the-best-way-to-deep-copy-an-object-in-javascript--cms-39655
*/
let a = {
user: 'A',
age: 99,
hobbies: {
sport: ['soccer', 'hockey'],
others: ['sleeping']
}
}
// only shallow copy
let b = {...a}
b.age = 50
b.user = 'B'
b.hobbies.sport = ['ping-pong']
b.hobbies.others = ['eating hamburgers']
// deep copy with nested
let c = JSON.parse(JSON.stringify(a));
c.age = 20
c.user = 'C'
c.hobbies.sport = ['swimming']
c.hobbies.others = ['tv & music']
console.log(a)
console.log(b)
console.log(c)