0

Would like to create array like this:

track[divID][wrapID]

I couldn't use track[divID][wrapID] = wrapID

cos some more will be added to 2nd dimension in another loop like this: track[divID][wrapID,wrapID2]

var track =[];
$("div").each(function() {

                        var wrapID = $(this).parent().attr('id')
                        var divID  = $(this).attr('id')

                        track[divID].push(wrapID)

                                                    });

the Error is "Uncaught TypeError: Cannot read property 'push' of undefined "

What did I do wrong ? Many thanks.

1
  • 1
    FYI track[divID][wrapID,wrapID2] would just evaluate to track[divID][wrapID2] due to how the comma operator works Commented Dec 30, 2014 at 17:09

1 Answer 1

5

You haven't created a new array for the second dimension:

track[divID] = [];
track[divID].push(wrapID);

Otherwise track[divID] is undefined and doesn't have push method.

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

1 Comment

I'd suggest track[divID] = track[divID] || [] to make sure he doesn't overwrite an existing array, since he says he's going to add more.

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.