我希望能够动态构建索引,以便可以使用按钮遍历对象属性。以下是我尝试过的,但不起作用。这怎么可能实现?
var bodyNr = 3;
var theBody = {
bodies: {
1: null,
2: null,
3: null
}
};
moveLeftBtn.on('click', function () {
bodyNr -= 1;
console.log(theBody.bodies["'" + bodyNr + "'"]); // undefined
console.log(theBody.bodies['2']); // works
});