0

I am to build two dimensional associated array but getting an error

while(phpcnt)
{
    pre_id=id
    pre_selected[track]['id']=some id;
    pre_selected[track]['price']=some no;
    console.log(pre_selected[track]['id']);
    console.log(pre_selected[track]['price']);
    phpcnt=phpcnt-1;
    track=track+1;
}

this is array declaraion var pre_selected=new Array();

and the error is TypeError: pre_selected[track] is undefined

5
  • Did you try this pre_selected['track']['id'] Commented Oct 16, 2015 at 6:42
  • this error TypeError: pre_selected[track] is undefined hints that, surprise!, pre_selected[track] is undefined, so you can't assign property to it Commented Oct 16, 2015 at 6:42
  • @RichS What is the difference between new Object() and object literal notation? Commented Oct 16, 2015 at 7:01
  • 1
    JavaScript doesn't have associative arrays. The closest thing to PHP's associative arrays are Objects. Please see the linked duplicate for more details. Commented Oct 16, 2015 at 7:03
  • @RichS, in your comment you suggest use new Object, but in most cases enough literal object, as more readable and in post by link from my comment also about this Commented Oct 18, 2015 at 6:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.