Javascript: How to insert a row in an index into a multidimensional array?

I am a beginner programmer in javascript. Please guide me on the right track.

Below is a 3 x 3 x 3 -dimensional array

"items": [ [ [1,2,3], [4,5,6], [7,8,9] ], [ [10,11,12], [13,14,15], [16,17,18] ] ]

Now I need to insert a row, for example, [ [19,20,21], [22,23,24], [25,26,27] ] with index 1 or 2 .

I tried with the splice function, but all in vain.

Being a multidimensional array, I do not know what value is included in the itemN parameter of the splice function.

items.splice(insertIndex, 0, `????`); 

How can i do this? Thanks

+4
source share
2 answers

splice deleteCount 0 ( ). thrid param 2- , .

var items = [ [ [1,2,3], [4,5,6], [7,8,9] ], [ [10,11,12], [13,14,15], [16,17,18] ] ];
var add = [ [19,20,21], [22,23,24], [25,26,27] ];
items.splice(insertIndex, 0, add);

: jsfiddle

+5

, splice, :

items.splice(2, 0,[ [19,20,21], [22,23,24], [25,26,27] ]);

DEMO

+1

Source: https://habr.com/ru/post/1612571/


All Articles