Muppet Muppet - 25 days ago 10
Javascript Question

Javascript Multidimensional Array is Empty

I think Im misunderstanding something here - I normally work in PHP and think I'm missing something small. My final array tmp is empty and displays as ",,,,,,,,,,,,,,,,". It seems to me my tmp array might be emptied somewhere or the scope gets reset for some reason. I'm using this as coordinates from a table where you can select table rows and posting to a webservice but my array seem to be erroneous.

var length = $("#arrayCount").html();
var letters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
var col = getSelectedColumn(); //for example sake lets say "B" is the selected column
var row = getSelectedRow(); //selected rows will be from "11" - "16"
var columnIndexStart = letters.indexOf(col[0]);
var tmp = [];
for(var i = row[0]; i <= row[1]; i++) //rows[0] = 11 and rows[1] = 16
{
tmp[i] = [];
for(var j = columnIndexStart; j < letters.length; j++) //columns and starts at index 1 if we work with "B"
{
var val = $("#" + i + "_" + letters[j]).html(); //using the row and letters as the associated DOM elements ID. Easier to retrieve it's HTML then.
if(val != undefined)
{
console.log("Index [" + i + "]['" + letters[j] + "'] = " + val); //works perfectly and prints as it should.
tmp[i]['"'+letters[j]+'"'] = val; //using quotes to save letters? Is this preferred?
}
}
}
console.log('Final Array: ' + tmp); //empty??
console.log('Final Array: ' + tmp[14]['G']); //testing HTML output. But is undefined.
return tmp;


Any help will be greatly appreciated.
Edited:
Example of console output.
enter image description here

Answer

My final array tmp is empty and displays as ",,,,,,,,,,,,,,,,"

With non-numeric index you are setting the field of object and not the element for index.

If you will have two-dimensional numeric array with numeric indices like the following:

var tmp = [[1,2,3], [1,2,3]];

after console.log('tmp = ' + tmp); you will obviously get the output string like:

tmp = 1,2,3,1,2,3

Because when you are trying to convert array to string it converts it elements to string and represent them with a commas.

However when you are trying to set element with non-numeric index, you are setting the field of this object.

var tmp = [];
tmp['A'] = 123;
console.log("tmp = " + tmp); // tmp = 
console.log(tmp.A); //123

So, console.log in your case works good - it is serializing all elements of two-dimensional array. But no one array of the second level does not have stored values, it has only fields, which are not included in the string representation of array.

You are getting a set of commas, because each sub-array of tmp array does not contains any element, so it's string representation is an empty string. Each sub-array contains the required data into it's fields.

When you are performing sum operation of string and object you are forcing object to convert to string representation. Instead of this it is recommended to use console.log(yourObj) - it will log the whole object without converting it to string.

//using quotes to save letters? Is this preferred?

No, "A" and A are different identifiers.

var s = new Object();
s['"A"'] = 123;
console.log(s['A']); //undefined
console.log(s['"A"']); //123

Additionally, if you will set fields with quotes - you can not get the field in normal style:

console.log(s."A"); //syntax error : expected identifier after '.'