I miss VB. Maybe that's sad...
I need to make a "3 Dimensional" array to represent the following data. The data begins as XML and gets parsed into a 2D "Array of arrays" as follows (descriptors added for clarity):
[NodeArray]
[Node: 0][Year: 2012][Quantity1: 3][Quantity2: 5] [Title: Orange]
[Node: 1][Year: 2012][Quantity1: 5][Quantity2: 9] [Title: Apple]
[Node: 2][Year: 2011][Quantity1: 4][Quantity2: 9] [Title: Orange]
[Node: 3][Year: 2011][Quantity1: 8][Quantity2: 12] [Title: Apple]
[Node: 4][Year: 2010][Quantity1: 2][Quantity2: 6] [Title: Orange]
[Node: 5][Year: 2010][Quantity1: 10][Quantity2: 2] [Title: Apple]
I want to parse this 2-D Array into a 3-D Object Array as follows:
[Year Array]
[2012]
[Quantity1]: [3],[7]
[Quantity2]: [5],[9]
[Title]: [Orange],[Apple]
[2011]
[Quantity1]: [4],[8]
[Quantity2]: [9],[12]
[Title]: [Orange],[Apple]
[2010]
[Quantity1]: [2],[10]
[Quantity2]: [6],[2]
[Title]: [Orange],[Apple]
In VB this would have been pretty simple. I would have looked at my original data and redimensioned my array to accomodate the maximum number of entries in each dimension. I would then iterate through the data and load it in...
If the 2012 index = 0 then "Array(0,1,1)" would have returned "9"
I believe this can be done in JSON, but I don't have enough experience to know how. I hope this question is specific enough to provide some insight on what I am trying to accomplish.
------------------EDIT & NEW CODE-----------------
I appreciate the answers.
I realize that my fatal flaw at the moment is that I am not building my 2-D array using JSON (despite the fact that my example seems that way). Here is the method I am using to build my 2-D Array:
for(i=0; i<titleArray.length; i++)
{
rowArray[i] = [yearArray[i], titleArray[i],quantityOneArray[i],quantityTwoArray[i]];
}
Because there are always the same number of entries in each array I can reliably use the length of one array to iterate through all associated arrays. I also realize that using "i++" is the middle school way of doing things, so I am open to suggestions on more elegant indexing methods.
But let's say I want to build this in JSON? I am confident that this is not yet in a JSON format... If it were then I should be able to say "rowArray[i].year" yes?
Thanks again - I am very fortunate to have such an active community.
--------------------EDIT2 & NEW CODE--------------------
Ok, I think I cracked the JSON nut. I likey! I only had to format the array object a little differently and now I can invoke members of the array by using the .whatever property!
for(i=0; i<titleArray.length; i++)
{
rowArray[i] = {"year":yearArray[i], "title":titleArray[i],"quantity1":quantityOneArray[i],"quantity2":quantityTwoArray[i]};
console.log("rowArray["+i+"]: " + rowArray[i].year + ", " + rowArray[i].title +", "+rowArray[i].quantity1+", "+rowArray[i].quantity2);
}
Now for the 3-D Array...