【问题标题】:How to replace .Id with a variable string value [duplicate]如何用变量字符串值替换.Id [重复]
【发布时间】:2021-11-13 09:51:34
【问题描述】:

我目前在我的 Javascript 代码中有以下内容,其中 Id 是对象的字段:

this.recId = event.detail.row.Id;

我想让 Id 部分通用

String a = 'Id';
this.recId = event.detail.row.a;

如何在 Javascipt 中实现这一点

【问题讨论】:

    标签: javascript variables generics field


    【解决方案1】:

    请改用[ ]

    const obj = {
      id: "myObject"
    }
    
    const a = "id"
    
    console.log(obj[a]);

    在你的例子中,它会是

    this.recId = event.detail.row[a];
    

    【讨论】:

    • this.recId = event.detail.row[a]
    【解决方案2】:

    你可以使用 this.recId = event.detail.row[a] 得到想要的结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-21
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 2018-12-30
      • 2022-08-09
      • 2020-03-22
      相关资源
      最近更新 更多