javascript - 如何设置 ' '内的值

标签 javascript arrays typescript

我有一些代码片段

 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/

相关文章:

arrays - 戈朗 :How to parse/unmarshal/decode a json array API response?

javascript - 有缺陷的二叉树

C++ - 字符数组和空字符

node.js - 关系 Y 的列 X 包含空值

javascript - 按住按钮连续滚动 ReactJS

javascript - 开 Jest : Share Variables Between Test Files

javascript - Chrome 和 IE 已解析/未解析 JSON 响应

javascript - Angular 8 : Prototype Inheritence with Date Object

javascript - 检查动态填充的数组中的重复对象

javascript - 如何使用循环在javascript中将数字增加0.01?