Is array.slice enough to handle a multidimensional array in JavaScript?

Is it enough array.sliceto clone a multidimensional array in JavaScript?

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(); });
+4
source share
2 answers

docs are pretty clear:

The method slice()returns a shallow copy of part of the array to a new array object.

, : slice , . . ( " ", ), .

+4

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-, ( - ).

+3

All Articles