Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript how to copy object without references

I have one object

item = { selectedItems: [] };

I did:

item1 = { ...item };
item2 = Object.assign({}, item);
item3 = Object.create(item);

but when I change selectedItems for item1 they are changed for all items. It's angular project What's wrong? Or what's right way? Or it's browser cache? I can't understand

like image 429
A. Gladkiy Avatar asked Apr 19 '26 08:04

A. Gladkiy


1 Answers

You need a deep copy, example:

let newItem = JSON.parse(JSON.stringify(item));
like image 180
Akif Hadziabdic Avatar answered Apr 20 '26 20:04

Akif Hadziabdic