0

I want to create a multidimensional array that will eventually look like this:

s
    [4]
        [3]
            [7][235,25903,502935],
            [8][2973,20385,97250],
            [9][293,2752,2935]
        [4]
            [7][28357,2057,923705],
            [8][2398,20597,20579],
            [9][275,23975,203795]

In PHP this is very easy to do, but in javascript I keep getting some BS about the 2nd dimension being undefined:

var s = [];
    s[4][3][7] = [23095,20753,2067];
    s[4][3][8] = [2664,86295,29357];

(that is not how I would do this in PHP btw)

1 Answer 1

2

You have to declare each nested array as an Array before you can assign values to it:

var s = [];
s[4] = [];
s[4][3] = [];
s[4][3][7] = [23095,20753,2067];
s[4][3][8] = [2664,86295,29357];
Sign up to request clarification or add additional context in comments.

4 Comments

You should note that iterating over this array may not be straightforward.
I assumed that the OP wasn't concerned about iterating over it, since the question title explicitly states that he wants "specific indices."
@Travesty: Ugh! I was afraid of that. what BS… Thanks!
Oh, ignore this comment—firebug just displayed it as [undefined,undefined[undefined]…] but when I fully expanded s everything was there. Thanks!

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.