I have a 2d JavaScript array like this:
inventory[1].products
where products is an object with elements
{"itemid":"na", "books":10, "dvds":15, "cds":4}
I need to be able to set the itemid with something like
inventory[1].itemid = "books"; indirectaddress = inventory[1].itemid;
and then be able to read the value of the books element with something like:
numberofbooks = inventory[1].indirectaddress;
Is this possible with JS arrays? I done some searching and can’t find anything, unless I am not using the correct nomenclature.
Advertisement
Answer
You can remember the property name in your indirectaddress variable, and use bracket notation to access “books” property via indirectaddress.
indirectaddress = "books"; numberofbooks = inventory[1][indirectaddress];