DEV Community

Discussion on: Mutate object or arrays without changing original source in javascript

Collapse
 
luisflins profile image
Luis Fernando Lins

Nice post! I'd also like to add that, if you don't want to add a new library just for that, you could use the spread operator, such as const newObj = {...originalObj}; and then, every time you change newObj, it won't affect originalObj anymore, as the spread operator makes a copy of the original object. The only caveat would be if you had other objects or arrays contained in the original object, and then the spread operator would have to be used in these elements as well.

Collapse
 
reachtokish profile image
Kishore

Thanks for reading!! You said it correctly about the nested thing for spread operator. I too like the spread operator and use it mostly but for the complex operations like the nested object you pointed, I use immutability-helper.