JavaScript经过变量设置对象键[重复] - JavaScript set object key by variable [duplicate]

问题:

This question already has answers here : 这个问题已经在这里有了答案
Closed 5 years ago . 5年前关闭。

I am building some objects in JavaScript and pushing those objects into an array, I am storing the key I want to use in a variable then creating my objects like so: 我正在用JavaScript构建一些对象并将这些对象推入数组,将要使用的键存储在变量中,而后像下面这样建立对象: 数组

var key = "happyCount";
myArray.push( { key : someValueArray } );

but when I try to examine my array of objects for every object the key is "key" instead of the value of the variable key. 可是,当我尝试检查每一个对象的对象数组时,键是"key"而不是变量键的值。 Is there any way to set the value of the key from a variable? 有什么方法能够经过变量设置键的值吗? app

Fiddle for better explanation: http://jsfiddle.net/Fr6eY/3/ 小提琴以得到更好的解释: http : //jsfiddle.net/Fr6eY/3/ ui


解决方案:

参考一: https://stackoom.com/question/mHsN/JavaScript经过变量设置对象键-重复
参考二: https://oldbug.net/q/mHsN/JavaScript-set-object-key-by-variable-duplicate