简体   繁体   English

如果初始属性是变量,如何访问 JSON object 的属性的属性?

[英]How to access property of a property of a JSON object if the initial property is a variable?

I am making a discord bot and have successfully made a system where it stores user's variables in a JSON file in this format:我正在制作一个 discord 机器人并成功制作了一个系统,它将用户变量存储在 JSON 文件中,格式如下:

{
  "801479782613123123": {
    "status": true
  },
  "725581416720629863": {
    "status": true
  }
}

My current problem is that I am unable to check this 'status' property is true or false when I pass in a variable as the initial property.我当前的问题是,当我将变量作为初始属性传递时,我无法检查此“状态”属性是真还是假。 Here is my code:这是我的代码:

let statusPath = './commands/afkstatus.json';
    let statusRead = fs.readFileSync(statusPath);
    let statusFile = JSON.parse(statusRead); //Ready for use
    let userID = message.author.id
    if (message.mentions.users.first()) {
        let ping = message.mentions.users.first();
        let status = statusFile[userID.status];
        if (statusFile[userID.status]) {
            message.channel.send(`Message`);
        }
    }

When I run the code, there is no error message, the message simply does not get sent.当我运行代码时,没有错误消息,根本没有发送消息。

Try let status = statusFile[userID].status instead.尝试let status = statusFile[userID].status代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM