如何使用对象的键

时间:2016-05-03 09:52:59

标签: javascript

如果有:

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

我怎样才能做到这样的工作:

console.log(obj.select); // This would output: This is b

1 个答案:

答案 0 :(得分:2)

  

使用bracket notation



var obj = {
  a: 'This is a',
  b: 'This is b',
  c: 'This is c'
}
var select = 'b';
console.log(obj[select]);