In this plunk I have an example of an array of objects a
that I copy with slice()
to b
. I alter one of the objects in a
but it changes also b
. Isn't slice
supposed to copy the array, including its contents? I need a
and b
to have different pointers.
Javascript
var a = [{x1:1, x2:2}, {x1:3, x2:4}];
var b = a.slice();
a[1].x1 = 5;
console.log(b[1]);
this prints:
x1: 5
x2: 4