0

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...

2 Answers 2

3

The data as a JSON object would look like

var data = 
{
    "2011": {
        "Quantity1": [ 3, 7],
        "Quantity2": [ 5, 9],
        "Title": [
            "Orange",
            "Apple"
        ]
    },
    "2012": {
        "Quantity1": [ 4, 8],
        "Quantity2": [ 9, 12],
        "Title": [
            "Orange",
            "Apple"
        ]
    }, 
    ...
}

Notice I'm using an object for the first 2 levels of the structure, this is so you can index them with string. e.g, your Array(0,1,1) would become data["2011"]["Quantity2"][1].

Alternatively you could use array and have

var data = 
[
    [
        [ 3, 7],
        [ 5, 9],
        [ "Orange",
          "Apple" ]
    ],
    [
        [ 4, 8],
        [ 9, 12],
        [ "Orange",
            "Apple" ]
    ], 
    ...
]

You could then do data[0][1][1] which is a lot like your VB example.

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

Comments

1

Something like this should do it:

var year_array = new Array(); // empty array
for ( node in node_array )
{
    if ( !year_array.hasOwnProperty(node.year) ) // if year is not in array yet
    {
        year_array[node.year] = new Array(); // create an empty array for it
    }
    year_array[node.year].push({
        "quantity1": node.quantity1,
        "quantity2": node.quantity2,
        "title": node.title,
    });
}

All this does is go through the 2D array, and puts the values in the 3D array in the format presented. The only bit that's tricky is the if ( !year_array.hasOwnProperty(node.year) ) block, which initializes each year's entry the first time it comes up.

12 Comments

This looks like the ticket... I also realize that I am building my array incorrectly to utilize your code. I will add my routine in an edit above.
Ok - getting an unexpected syntax error; hopefully I can get the sample code in here ok: yearArray = {}; for(i=0; i<rowArray.length; i++){ if(!yearArray.hasownProperty(rowArray[i].year){ yearArray[rowArray[i].year]={}; Uncaught SyntaxError: Unexpected token [ } yearArray[rowArray[i].year].quantity1+=rowArray[i].quantity1; yearArray[rowArray[i].year].quantity2+=rowArray[i].quantity2; yearArray[rowArray[i].year].title+=rowArray[i].title; } Note the unexpected token error. I don't believe I am allowed to index this way...
@Chris: Try changing yearArray = {}; to var yearArray = new Array();.
Hmmm no change. I tried yearArray = new Array() and yearArray = []. My gut instinct is that I can't use rowArray[i].year as an index value for the yearArray object. Let's see if I try it a different way...
Meh, I made a typo; note the lack of a closing paren on the if(!yearArray.hasownProperty(rowArray[i].year){ statment. One more bug further down and then I'll see what I get!
|

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.