簡體   English   中英

如何在另一個數組內的數組中添加元素

[英]How to add a element to an array inside another array

這是我的情況。 我有以下數組。

arrParent = [[List1,A,B,C],[List2,E,F,G,H]];
arrChild1 = [List1,A,B,C];
arrChild2 = [List2,E,F,G,H];

我想在arrChild1的末尾添加“ D”。 我在使用javascript定位子數組時遇到困難。

我在用:

arrParent[0].push("D")

...但是結果不是我想要的。 我得到的結果是:

arrChild[(List1,A,B,C),D];

我可以驗證此為我的來電:

console.log(arrChild1[0] (which displays List1,A,B,C)
console.log(arrChild1[1] (which displays D)

我需要:

arrChild1[0] = List1
arrChild1[1] = A
arrChild1[2] = B
arrChild1[3] = C
arrChild1[4] = D

有人可以指出我正確的方向嗎? 提前致謝。

var foo = [1,2,3]; // access like: foo[0] === 1

var bar = [foo, [4,5,6]];
var baz = bar[0]; // baz[0] === 1 same as foo[0]

//so to access an array within array
var fuz = bar[0][0]; // fuz === 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM