[英]Javascript: How to insert a row at an index to a multidimensional array?
我是 JavaScript 的新手程序員。 請引導我走上正確的道路。
下面是一個大小為2 x 3 x 3的 3D 數組
"items": [ [ [1,2,3], [4,5,6], [7,8,9] ], [ [10,11,12], [13,14,15], [16,17,18] ] ]
現在,我需要在索引 1 或 2 處插入一行,類似於[ [19,20,21], [22,23,24], [25,26,27] ]
。
我嘗試了拼接功能,但都是徒勞的。
作為一個多維數組,我不知道splice函數的itemN參數中有什么值。
items.splice(insertIndex, 0, `????`);
我怎樣才能完成它? 謝謝
使用帶有 deleteCount 0(第二個參數)的 splice。 作為第三個參數,您輸入要添加的二維數組。
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
接受的答案很好。
我做了一個演示,以角度插入中間行作為參考。
我為誰擔心。
textarray = [
[{ text: "Content 1" }, { text: "Content 2" }],
[{ text: "Content 3" }, { text: "Content 4" }]
];
rowindex = 1;
insertmiddle() {
let size = this.textarray[0].length;
this.textarray.splice(this.rowindex, 0,
Array(size).fill({ text: "Content", tag: "Content" }));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.