javascript dynamically populate associative array and get values

not quite, try this:

 for(var i=0; idArray.length; i++)
 {
    myAssociativeArr[i] = {
                           id: idArray[i], 
                           lname: lnameArray[i], 
                           fname: fnameArray[i]
                          };
 }

to get the id of the 5th element: myAossociativeArr[i]['id'], I'm sure you can figure out the rest from here ;)


You are very close. First of all, if you wish to use the array subscript notation, you have to pass the keys themselves (strings in your case, like this):

var myAssociativeArr = [];
for (var i=0; i < idArray.length; i++) {
    var newElement = {};
    newElement['id'] = idArray[i];
    newElement['lname'] = lnameArray[i];
    newElement['fname'] = fnameArray[i];
    myAssociativeArr.push(newElement);
}

Where the key names are known strings, it's often preferable to use the completely equivalent notation of object properties:

var myAssociativeArr = [];
for (var i=0; i < idArray.length; i++) {
    var newElement = {};
    newElement.id = idArray[i];
    newElement.lname = lnameArray[i];
    newElement.fname = fnameArray[i];
    myAssociativeArr.push(newElement);
}

You can be even more concise by using object literals, as you did in your sample output:

var myAssociativeArr = [];
for (var i=0; i < idArray.length; i++) {
    myAssociativeArr.push({
        id: idArray[i],
        lname: lnameArray[i],
        fname: fnameArray[i]
    });
}

Edit: fixed loop indexing to not be infinite.

You read elements the same way you write them: myAssociativeArr[i]['id'] etc., or myAssociativeArr[i].id etc.

For lookups by ID, it's a good idea to construct an object for this.

var myObject = {};
for (var i=0; i < idArray.length; i++) {
    myObject[idArray[i]] = {
        id: idArray[i],
        lname: lnameArray[i],
        fname: fnameArray[i]
    };
}

To look up:

myObject['2'] // => { id: '2', ... }

for(var i=0; idArray.length;i++)
    {
    myAssociativeArr [i][id]=idArray[i];
    myAssociativeArr [i][lname]=lnameArray[i];
    myAssociativeArr [i][fname]=fnameArray[i];
    }