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
相關文章
相關標籤/搜索