例如,对象的属性名称有一个数字,我想为遍历对象添加一个属性,该属性名称必须有一个数字。
打印结果如下:
[{ room[1]: 1 }, { room[2]: 2 }, { room[3]: 3 }, { room[4]: 4 }, { room[5]: 5 }, { room[6]: 6 } ]
var = arr[] for (let index = 0; index < 10; index++) { arr.push({ `room[${index}]`: index, }) } console.log(arr)
查看更多
缺少所谓的计算属性名称:
假设变量声明中有错字 arr
arr
var arr = []; for (let index = 0; index < 10; index++) { arr.push({ [`room[${index}]`]/*Computed-property names*/: index, }); } console.log(arr)
.as-console-wrapper { min-height: 100%; }