166
rated 0 times
[
170]
[
4]
/ answers: 1 / hits: 17400
/ 7 Years ago, fri, january 12, 2018, 12:00:00
I'm trying to clone Array to a new one, and I want cloned array has no reference to original copy
I know there're splice
and from
method, but new array from those methods all has reference to original array
e.g.
let original = [ [1,2], [3,4] ];
let cloned = Array.from(original); // this will copy everything from original
original[0][0] = -1;
console.log(cloned[0][0]); // the cloned array element value changes too
I also tried using loop to have cloned[i][j] = original[i][j]
, but result is the same
How can I create a new Array with every element having same value from original Array, but the cloned Array should have no reference to original Array?
thanks!
More From » javascript