This question already has an answer here:
- Copying array by value in JavaScript 16 answers
I want to form an array from an existing array so I can modify the new array without affecting the old. I realise arrays are mutable and this is why the new array affects the old.
E.g.
old = ["Apples", "Bananas"];
new = old;
new.reverse();
Old has also been reversed.
In Python, I can just do new = list(old)
, but doing new = new Array(old);
puts the old list inside a list.
var arrOld = ["Apples", "Bananas"]; var arrNew= [...arrOld];
– GibboK Aug 11 at 20:36