1

I have an array of arrays. Say an array of fruits, and each fruit array has an array of properties. Something like

[["Apple", "seedless", "red"],["Banana", "seedless", "yellow"]]

I now have another array which has an additional property of each of the fruits in the same order as the fruits. say my other array is ["sour","sweet"]. The sour property is to be added to the apple array of properties and sweet is to be added to the banana array of properties so the resultant array looks like

[["Apple", "seedless", "red", "sour"],["Banana", "seedless", "yellow", "sweet"]] 

How do I get to the inner array and append it? I know in the inner array I just have to do

for(var i=0; i<tastePropArray.length; i++){
innerArray.push(tastePropArray(i));
}

but How do I reach/access that inner array?

4

2 回答 2

5

尝试这个:

var fruits = [["Apple", "seedless", "red"],["Banana", "seedless", "yellow"]];
for(var i=0; i<fruits.length; i++){
    fruits[i].push(tastePropArray[i]);
}

但我会提出一个更好的数据模型:

像这样储存水果:

var fruits = {
    "Apple": {
         "seeds": "no",
         "colour": "red",
         "taste": "sour"
    },
    "Banana": {
         "seeds": "no",
         "colour": "yellow",
         "taste": "sweet"
    }
};

console.log(fruits.Apple.taste); // sour

添加水果,例如:

fruits.StrawBerry = {
    "seeds": "yes",
    "colour": "red",
    "taste": "sweet"
}

使用 afor .. in在水果上循环。

于 2013-11-05T14:51:14.867 回答
1
var a = [["123","234"],["asd","fff"]]

a[0] // prints ["123", "234"]
a[0][0] // prints "123"

抱歉,我误解了@Frits van Campen 的问题,答案是绝对正确的。

于 2013-11-05T14:51:28.387 回答