Friday, May 10, 2024
33
rated 0 times [  36] [ 3]  / answers: 1 / hits: 173161  / 8 Years ago, wed, september 28, 2016, 12:00:00

Googling for javascript clone object brings some really weird results, some of them are hopelessly outdated and some are just too complex, isn't it as easy as just:



let clone = {...original};


Is there anything wrong with this?


More From » ecmascript-6

 Answers
5

This is good for shallow cloning. The object spread is a standard part of ECMAScript 2018.



For deep cloning you'll need a different solution.



const clone = {...original} to shallow clone



const newobj = {...original, prop: newOne} to immutably add another prop to the original and store as a new object.


[#60576] Sunday, September 25, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dorothylorrainef

Total Points: 456
Total Questions: 102
Total Answers: 115

Location: El Salvador
Member since Sun, Sep 12, 2021
3 Years ago
;