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 much appreciated.

You are expecting javascript arrays to behave like PHP arrays. They don't!

Elements of a JS array are indexed exclusively by integer.

Confusingly, JS arrays can also be given properties, so tmp[14]['G'] = foo is quite legal but not what you might expect from knowledge of PHP. ['G'] is a property of tmp[14] without being an array element!

This all stems from the way JS implements arrays. Array is heavily based on Object but gives special status only to integer keys.

Straightforwardly you could do as follows :

// change
tmp[i] = [];
// to
tmp[i] = {}; // a js plain object

// change
tmp[i]['"'+letters[j]+'"'] = val;
// to
tmp[i][letters[j]] = val;

However, JS plain objects are just collections of properties ... without order ..., which may suffice.

But if order is important, and you want to convey both a key (letter) and a value (val), then you need an array of objects, each containing a key|value pair.

// keep
tmp[i] = []; // arrays have order.

// change
tmp[i]['"'+letters[j]+'"'] = val;
// to
tmp[i].push({ 'key':letters[j], 'value':val });

Love it or loath it, that's javascript.

commented: Masterclass :) +15

Thank you for the detailed reply Airshow. This makes a lot more sense to me now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.