简体   繁体   English

无法访问对象的属性-对象

[英]Cannot access object's properties - objects

I'm working on a discord bot of mine and I was making a "help" command. 我正在开发我的不和谐机器人,并且正在执行“帮助”命令。 I first had a commands array and if they wanted help on a specific command I had to add more lines of code than I want to. 我首先有一个commands数组,如果他们想要特定命令的帮助,我必须添加比我想更多的代码行。 I was wondering if I could do put an object inside of my object like this: 我想知道是否可以像这样将对象放入对象内部:

const commands1 = {
    coinflip: {
        usage: prefix + "coinflip",
        description: `Flip a coin then guess what it landed on.`
    }
    diceroll: {
        usage: prefix + "diceroll (number)",
        description: `Roll a die and guess what it lands on`
    }
};

Or would I have to do something else, because when I do 还是我必须做其他事情,因为当我做

for(var name in commands1){
    embed.addField("Command:", name)
}

this will list all the commands available. 这将列出所有可用的命令。 However I can't access the usage or description, I tried this by doing this: 但是,我无法访问用法或说明,我通过执行以下操作尝试了此操作:

.addField("Usage:", name.usage)
.addField("Description:", name.description)

(it says undefined) Am I accessing it wrong or can I not put objects in objects. (它表示未定义)我访问错误还是无法将对象放入对象中? Sorry, I'm relatively new to this :) Thanks. 抱歉,我对此还比较陌生:)谢谢。

I found out that name. 我发现了那个名字。 is literal and it thinks I'm trying to access commands1.name when I wanted commands1.coinflip. 是字面意义,它认为我想要使用commands1.coinflip时尝试访问commands1.name。 So I fixed it by doing this 所以我通过这样做解决了

console.log(commands1.coinflip.usage)

You are using for...in loop, which iterates over the indexes of the array. 您正在使用for...in循环,该循环遍历数组的索引。 But the real scenario is you have object . 但是真正的情况是你有object So, in this case i would suggest you this: 因此,在这种情况下,我建议您这样做:

const commands1 = {
    coinflip: {
        usage: prefix + "coinflip",
        description: `Flip a coin then guess what it landed on.`
    }
    diceroll: {
        usage: prefix + "diceroll (number)",
        description: `Roll a die and guess what it lands on`
    }
};

const keys = Object.keys(commands1); // #Output : ["coinflip", "diceroll"]

for(let key of keys){
   embed.addField("Command:", commands1[key].usage);
}

Don't worry about being new, we all started somewhere. 不用担心成为新手,我们都从某个地方开始。

Your newbie questions are probably better than mine were! 您的新手问题可能比我的要好!

const commands1 = {
    coinflip: {
        usage: prefix + "coinflip",
        description: `Flip a coin then guess what it landed on.`
    },
/* Added a missing , above */ 
    diceroll: {
        usage: prefix + "diceroll (number)",
        description: `Roll a die and guess what it lands on`
    }
};

for(var name in commands1){
    embed.addField("Command:", name);
    console.log(commands1[name].usage);
    console.log(commands1[name].description); /* You can Use your index to directly access the object thru the parent object. */ 
}

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

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