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.
  • 166,899
  • 29
  • 327
  • 400
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,453
  • 9
  • 36
  • 68