Jump to content

Javascript multi dimensionals array with keys


Drongo_III

Recommended Posts

Hi Guys

 

Super simple question. I want to create a multidimensional JS array where I can define the keys. Perhaps my mistake is using similar syntax to php but the following code throws an error in chrome console saying "Uncaught TypeError: Cannot set property 'top' of undefined "

 

Can anyone nudge me in the right direction on this?

 

var locObj = [];
locObj[1]['top'] = 200;
alert(locObj[1]['top']);

 

 

Worked it out now.

 

Seems you have to declare that the first array level is an array too:

 

var locObj = [];
locObj[1] = [];
locObj[1]['top'] = 'twinky'

alert(locObj[1]['top']);

 

 

Just thought I would post back in case anyone else gets stuck on something similar.

 

Though why you can't just write it like php is hard to fathom - still that's javascript for ya!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.