I want to build an associative array based on an array and then get the values of that associative array. The structure of the associative array is as follows:
var myAssociativeArr = new Array();
myAssociativeArr = [
{ id:'1',
lname:'doe',
fname:'john'
},
{ id:'2',
lname:'smith',
fname:'john'
}
]
I have three arrays of the same length from which I will build this associative array i.e. id, lname and fname array.
for(var i=0; idArray.length;i++)
{
myAssociativeArr [id]=idArray[i];
myAssociativeArr [lname]=lnameArray[i];
myAssociativeArr [fname]=fnameArray[i];
}
Please tell me if the above approach is correct, if not how can I build this array and also how can I get the values of this array via a loop.
Your help will be appreciated.
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 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With