javascript - 如何将变量 X 替换为数组索引 0 处的数据,其值为 'X'

标签 javascript object for-loop promise

    this.work[0].minValue = await this.getData().then(
      value => value[0].set.min.X
    )
    this.work[1].minValue = await this.getData().then(
      value => value[0].set.min.Y
    )
    this.work[2].minValue = await this.getData().then(
      value => value[0].set.min.Z
    )
    this.work[3].minValue = await this.getData().then(
      value => value[0].set.min.A
    )
    this.work[4].minValue = await this.getData().then(
      value => value[0].set.min.B
    )
    this.work[5].minValue = await this.getData().then(
      value => value[0].set.min.C
    )

我试图在循环内编写这 6 行代码,但我无法做到。有人可以帮我吗?

var arr = ['X', 'Y', 'Z', 'A', 'B', 'C']
    var i = 0
    var that = this
    this.work.forEach(item => {
      item.minValue = that.getData().then(value => value[0].set.min.that.arr[i])
      i++
    })

我知道 min.that.arr[i] 这是错误的。请不要说出它不起作用的原因,请帮我解决问题。

最佳答案

value[0].set.min[arr[i]]

ashkan 的建议解决了我的问题。

关于javascript - 如何将变量 X 替换为数组索引 0 处的数据,其值为 'X',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59874071/

相关文章:

javascript - 如何通过名称获取html元素

object - golang 发送和接收对象

javascript - 使用Webpack编译js文件时出现 "Uncaught TypeError"

javascript - 无法使用ajax上传文件

java - 如何将java多类小程序转换为html文件

Python 无法引用调用类中的列表

javascript - 添加事件监听器,for(),索引。如何使用闭包?

c - 尝试抓取 C 中空格后的字符

powershell - 从网络位置复制列表中定义的文件

javascript - 在调用 s.t() 之前检索 Omniture 跟踪调用 URL