1

So I have

var arrays = [
    [ Material A, 1, 2, 3, 4, 5  ],
    [ Material B, 6, 7, 8, 9, 10  ],
    [ Material C, 11, 12, 13, 14, 15  ]
];

and I need to put it in this format which is a multidimensional associative array,

var bigdata = [
    { "Name": "MaterialA", "Row1": 1, "Row2": 2, "Row3": 3, "Row4": 4, "Row5": 5 },
    { "Name": "MaterialB", "Row1": 6, "Row2": 7, "Row3": 8, "Row4": 9, "Row5": 10 },
    { "Name": "MaterialC", "Row1": 11, "Row2": 12, "Row3": 13, "Row4": 14, "Row5": 15 }
];

I am trying

var bigdata = new Array(3);         

for (i=0; i<3; i++)
{
    // do name
    bigdata[i][0] = {"Name" : arrays[i][0]};

    for (j=1; j<6; j++ )
    {
        // rest of that row         
    }
}

But so far it is not working when I try to store the first "Name": "MaterialA" . What am I doing wrong or can this even be done? Thanks for the help.

3
  • I don't see commas separating the inner-most arrays in the arrays var. And why have single element arrays? Commented Jun 26, 2014 at 14:09
  • Is this exact syntax for your array definition? I mean, you have three level array the way you've declared it. Commented Jun 26, 2014 at 14:09
  • @bloodyKnuckles yes sorry about that, I forgot the commas, I will fix that now. Commented Jun 26, 2014 at 14:13

2 Answers 2

2

This is working for me. Notice I removed the [0] from your bigdata[i][0], and added the row assignment code to your "j" loop.

for (i=0; i<3; i++)
{
    // do name
    bigdata[i] = {"Name" : arrays[i][0]};

    for (j=1; j<6; j++ )
    {
        // rest of that row
        bigdata[i]['Row' + j] = arrays[i][j];
    }
}

JSFiddle: http://jsfiddle.net/ub54S/1/

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

3 Comments

Yes thank you. Also you were right earlier about the single element arrays. I thought they were single element arrays but are instead separated by commas. I fixed it in the original post to reflect the suggestion.
I updated my response. Also, in your example, the "Material A/B/C" array elements are not quoted. I assume that's a typo in your post too.
Actually they are unquoted in arrays but then when they get put into bigdata they need to have quotes surrounding them.
1

The proper way to set a property of an associative array/object is like this:

bigdata[i]["Property"] = value   // this allows dynamic property name
bigdata[i].Property = value      // or like this, if property is hard-coded

So in your case, it should be:

bigdata[i] = {}   // initialize a blank object
bigdata[i].Name = arrays[i][0];

for ( j=1; j<6; j++ )
    bigdata[i]["Row" + j] = arrays[i][j];

Here is a demo: http://jsfiddle.net/56tk5/

4 Comments

Thanks that definitely helped. However when I try to access the first spot in bigdata, I use bigdata[0] and it returns [object Object]. I thought it would say "Name": "MaterialA". Am I accessing that incorrectly?
If you want to reference a specific value, use the same code - bigdata[i].Name. If you are trying to output the contents in a readable way, you can use something like JSON.stringify. The bigdata object has the equivalent contents as the example you provided in your post - it just doesn't display that way if you directly dump it out.
The bigdata example in the question is an array, not an object.
The object bigdata is an array, however each entry in the array bigdata[i] is an object.

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.