如何在另一个数组内的数组中添加元素 [英] How to add a element to an array inside another array
本文介绍了如何在另一个数组内的数组中添加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的情况。我有以下数组。
Here is my situation. I have the following 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定位子数组时遇到困难。
I want to add "D" to the end of arrChild1. I am having difficulty targeting the child array in my javascript.
我正在使用:
arrParent[0].push("D")
。 ..但是结果不是我想要的。我得到的结果是:
...but the result is not what I want. The result I get is:
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
有人可以指出正确的方向吗?
Can someone point me in the right direction? Thanks in advance.
推荐答案
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
这篇关于如何在另一个数组内的数组中添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文