我有一些代码片段
for(var i=0; i < this.arr.length; i++) {
arr.push({
id: 'i',
label: this.arr[i],
display: () => this.arr[i]
})
}
我想知道如何在每次迭代的“”内设置值。我想要类似 '1' 、 '2' 、 '3' .....
最佳答案
最简单的方法是使用 .toString()
方法。
for(var i=0; i < this.arr.length; i++) {
arr.push({
id: i.toString(),
label: this.arr[i],
display: () => this.arr[i]
})
}
示例
let arr = [];
for(var i = 0; i < [1, 2, 3, 4].length; i++) {
arr.push({
id: i.toString()
})
}
console.log(arr)
关于javascript - 如何设置 ' '内的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45738715/