I’m trying to do this for a multidimensional array and can’t figure out why it doesn’t work for a single dimensional array:
function doArray() {
var msg = [];
msg.push({entryID : "test"});
alert(msg[entryID]);
}
I get undefined when I try to alert it. What’s wrong here? Thanks.
First, you have to use the index for the array (
[0]), and then you need to quote the keyname to access the value in the object you’ve pushed onto the array:http://jsfiddle.net/CgbTC/
Try it with a
console.log(msg)to see what you actually have:http://jsfiddle.net/CgbTC/1/
Also, as tskuzzy notes, you can access using
msg[0].entryID, asmsg[0]will give you the object you’ve added to the array:http://jsfiddle.net/CgbTC/3/