Is it enough array.sliceto clone a multidimensional array in JavaScript?
array.slice
For instance:
var array = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; var b = array.slice(); console.log(b);
I saw a secondary implementation like this from Play by Play: Lea Verou in multiple terms:
b = array.slice().map( function(row){ return row.slice(); });
docs are pretty clear:
The method slice()returns a shallow copy of part of the array to a new array object.
slice()
, : slice , . . ( " ", ), .
slice
MDN:
slice() .
, /. :
var array = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; var b = array.slice(); b[0].push(10); console.log(array); > [ [1, 2, 3, 10], [4, 5, 6], [7, 8, 9] ]
, 2D-, ( - ).