sthor69 sthor69 - 11 months ago 55
HTML Question

associative multidimensional array in javascript

I'm trying to use an associative multi-dimensional array in javascript, but there is something wrong in the code.


<p onclick="myFunction()">Test</p>


myFunction = function() {


obj['temp']['room'] = 1;



The result is that only the first alert is fired.
What is wrong in the definition of the array?

Answer Source

You need to declare the variable and initialize it as object, then you can assign a value.

var obj = { temp: {} };
obj['temp']['room'] = 1;