Skip to content
Advertisement

Is there a way to use “indirect addressing” for JavaScript arrays?

I have a 2d JavaScript array like this:

JavaScript

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

JavaScript

and then be able to read the value of the books element with something like:

JavaScript

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.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement