【问题标题】:How to get my variable works as a variable and not a property of object?如何让我的变量作为变量而不是对象的属性工作?
【发布时间】:2022-01-16 11:05:59
【问题描述】:

这是交易

   if(!userExist) {
          userJson["user"] = [{"name": msg.author.username, "commands": { userCommand : userCommandValue }}]
    }

编辑: 为避免混淆,这是我的 userCommand :

让 userCommand = msg.content;

userCommand 被视为一个属性,而不是一个现有的变量...

有没有办法绕过它?

【问题讨论】:

  • userCommand 是你代码中的一个属性
  • userCommand 在我的代码中是这样定义的:let userCommand = msg.content;
  • 您想将userCommand 属性视为变量吗?您必须“访问”userJSON["user"] 对象中的 userCommand 属性并手动将其分配给变量。
  • 啊,从你最近的评论来看,我有点不对劲。这个问题有点混乱。

标签: javascript node.js json


【解决方案1】:

从 ES6 开始,您可以这样做:包装 [] arount userCommand

if(!userExist) {
      userJson["user"] = [{"name": msg.author.username, "commands": { [userCommand] : userCommandValue }}]
}

【讨论】:

  • 这个特性被称为计算属性
  • 我的男人!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-23
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多