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