0

var myObj = {
    test: 0,
    other: 'none'
};
var readItem = 'test';
console.log(myObj.readItem);

How would I be able to hold, in a variable, the name of an item in an object and then use that variable to call the item? Thanks!

Luke Strong
  • 19
  • 1
  • 3

2 Answers2

0

Can be looked up via the indexer:

myObj[readItem]

var myObj = {
    test: 0,
    other: 'none'
};
var readItem = 'test';
console.log(myObj[readItem]);
H.B.
  • 142,212
  • 27
  • 297
  • 366
0

You can access the object property by following pattern

myObj[readItem];

var myObj = {
    test: 0,
    other: 'none'
};
var readItem = 'test';
console.log(myObj[readItem]);
Ayaz Ali Shah
  • 3,305
  • 8
  • 35
  • 64