You need to make the object first, then use []
to set it.
(您需要先创建对象,然后使用[]
进行设置。)
var key = "happyCount";
var obj = {};
obj[key] = someValueArray;
myArray.push(obj);
UPDATE 2018:
(更新2018:)
If you're able to use ES6 and Babel, you can use this new feature:
(如果您能够使用ES6和Babel,则可以使用此新功能:)
{
[yourKeyVariable]: someValueArray,
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…