sthor69 sthor69 - 1 month ago 9
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.

html

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


javascript

myFunction = function() {

alert(1);

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

alert(2);

}


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

Answer

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

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