I have the following code:
const list = [{
items: [
"2",
"3",
],
}, {
items: [
"1",
],
}, ];
const selectedList = 0;
const newList = [...list];
newList[selectedList].items = [...list[selectedList].items, "4"];
console.log(list); // X [ { items: [ '2', '3', '4' ] }, { items: [ '1' ] } ] // <-- why does it have 4 inside?
console.log(newList); // ✓ [ { items: [ '2', '3', '4' ] }, { items: [ '1' ] } ]
I expect list to be [ { items: [ '2', '3' ] }, { items: [ '1' ] } ]
and newList [ { items: [ '2', '3', '4' ] }, { items: [ '1' ] } ]
. Why does list have the '4' as well? I'm spreading the list and not chaning the items of the original list?