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

Cœur
  • 37,241
  • 25
  • 195
  • 267
rohan
  • 43
  • 11
  • Did you try this `pre_selected['track']['id']` – Osama Mohamed Oct 16 '15 at 06: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 '15 at 06:42
  • @RichS [What is the difference between `new Object()` and object literal notation?](http://stackoverflow.com/questions/4597926/what-is-the-difference-between-new-object-and-object-literal-notation) – Grundy Oct 16 '15 at 07: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. – Madara's Ghost Oct 16 '15 at 07: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 '15 at 06:32

0 Answers0