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'] 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
    – Grundy
    Oct 16, 2015 at 6:42
  • 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. 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
    – Grundy
    Oct 18, 2015 at 6:32

0