Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am making a program that generates a Javascript file. I have never worked with Javascript, so I may be asking a stupid question :) Anyhow, this Javascript files gets initialized with data.

Here is an example line where I initialize an entry in an array with an object that contains two properties whereof one of them is an array containing 3 objects...

Or, well, that was the idea at least... Is there anyway I can do so so "M" becomes a real array that can be indexed normally?

W1[242] = {"W":"authors","M":{ "ArrItem0":{"U":32,"S":4.37},"ArrItem1":{"U":38,"S":4.02},"ArrItem2":{"U":406,"S":1.53} } };
share|improve this question
add comment

3 Answers

up vote 3 down vote accepted
W1[242] = {"W":"authors","M":[ {"U":32,"S":4.37},{"U":38,"S":4.02},{"U":406,"S":1.53} ] };
share|improve this answer
    
Thanks everyone! :) –  Tom Nov 18 '11 at 13:34
add comment

The array notation is the following, with brackets []:

W1[242] = 
    { 
       "W": "authors",
       "M": [{"U":32,"S":4.37}, {"U":38,"S":4.02},{"U":406,"S":1.53}]
    };
share|improve this answer
add comment

Depends on how you want to work with what is in M. Here is one option where each element of M is it's own object.

W1[242] = {"W":"authors","M":[ {"U":32,"S":4.37},{"U":38,"S":4.02},{"U":406,"S":1.53} ] };
share|improve this answer
    
He wants non-associative array for M. –  Jonathan M Nov 18 '11 at 5:41
    
Yep, noticed after the fact. –  Bert Evans Nov 18 '11 at 5:44
    
No worries. All good now. –  Jonathan M Nov 18 '11 at 5:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.