我有一个JavaScript数组:如何通过JavaScript中的索引来引用数组项目?
Fruit > Banana > Colour > Yellow
> Size > Large
> Cost > $3.50
> Apple > Colour > Green
> Size > Small
> Cost > $0.42
我可以用得到的值:
alert(Fruit['Banana']['Colour']);
我如何使用索引相同的值?例如
alert(Fruit[0][0]);
谢谢大家你们导致我在正确的方向,这里是解决方案,我是后:
for (var a in Fruit) {
//var a gives me "Banana" and "Apple"
for (var b in Fruit[a]){
//var b gives me "Colour", "Size" and "Cost"
//Fruit[a][b] gives me the values
}
}
你在寻找一个关联数组吗? – fncomp
我看到你正在使用一个对象,但也想使用索引。正如@Edgar Villegas Alvarado所提到的,这不是本地特征。 – fncomp