So why myarray[bla][bl] always equal to NaN? If I do the same thing with 1 dimension (myarray[bla]), I get the number.
var bla = 'blabla';
var bl = 'bla';
var myarray = [];
for (i = 0; i < 10; i++) {
if (!myarray[bla]) {
myarray[bla] = [];
}
myarray[bla][bl] += i;
console.log(myarray[bla][bl] + " + " + i);
}
Ok, so let’s step through your loop, replacing instances of the variable
blawith the string value of'blabla':Arrays in javascript are index by integer values. What your code here is doing is adding an expando property to the array instance named
blabla. That is:now reconsider your increment statement:
or, with the expando properties:
What this is trying to do is access the property named
blon the empty array. That’s why you’re gettingundefinedhere.Anyway, as a best practice, you might want to avoid using arrays in javascript like hashtables, since problems like this are bound to crop up after enough time.