0

If there is:

var obj = {a: 'This is a', b: 'This is b', c: 'This is c'}
var select = 'b';

How can I make to work something like this:

console.log(obj.select); // This would output: This is b
Cristian
  • 1,590
  • 5
  • 23
  • 38
  • Possible Duplicate of [Using variable keys to access values in JavaScript objects](http://stackoverflow.com/questions/922544/using-variable-keys-to-access-values-in-javascript-objects) – Tushar May 03 '16 at 09:55

1 Answers1

2

Use bracket notation

var obj = {
  a: 'This is a',
  b: 'This is b',
  c: 'This is c'
}
var select = 'b';
console.log(obj[select]);
Jed Fox
  • 2,979
  • 5
  • 28
  • 38
Rayon
  • 36,219
  • 4
  • 49
  • 76