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?

2
  • 1
    Try nested loop accessors outerArray[i][j] or just use objects, which would seem to be more suited to what you need. Commented Nov 5, 2013 at 14:51
  • Using array objects would be better in this case I suppose.. Commented Nov 5, 2013 at 14:53

2 Answers 2

5

Try this:

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

But I will propose a better datamodel:

Store your fruits like:

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

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

Add fruits like:

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

Use a for .. in to loop over the fruits.

Sign up to request clarification or add additional context in comments.

1 Comment

I like your first answer, that is most relevant for me. However thanks a lot, more knowledge about programming never hurts !
1
var a = [["123","234"],["asd","fff"]]

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

Sorry i misunderstood the question @Frits van Campen answer is absolutely right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.