[英]How to dynamically add properties to an object in node.js?
我有一个对象,我希望能够在其中动态添加至少一个属性。 属性的数量取决于我拥有的数组中的项目。 这是我引用的对象:
const helpEmbedMsg = new global.Discord.MessageEmbed()
// Display a different (random) color every time the command is used.
.setColor("RANDOM")
.setTitle("Help")
.setAuthor("Beatrice~")
.setDescription("A full list of the commands available to you..")
.addFields(
{ name: `The current __prefix__ for this server is: \`${prefix}\``, value: "\u200B" },
)
.addField("📋 Here's a list of all my commands:", "\u200B", false)
.addField("\u200B", "\u200B", false)
.addField(`Type \`${prefix}help <command>\` to learn more about a command and it's usage! `, `Example: \`${prefix}help ping\``, false)
.setTimestamp()
.setFooter(`Command triggered in ${message.channel.guild}`);
所以,基本上,如果数组的长度是 4,我想添加以下内容:
.addField(array[i], "text here", true)
四次,但在对象中的特定位置(顺序计数),就在此行下方:
.addField("📋 Here's a list of all my commands:", "\u200B", false)
因此,只需将代码分成两个链,并在它们之间使用for
循环:
const helpEmbedMsg = new global.Discord.MessageEmbed();
helpEmbedMsg.setColor("RANDOM")
.setTitle("Help")
.setAuthor("Beatrice~")
.setDescription("A full list of the commands available to you..")
.addFields(
{ name: `The current __prefix__ for this server is: \`${prefix}\``, value: "\u200B" },
)
.addField("📋 Here's a list of all my commands:", "\u200B", false);
// add an array of properties in this specific order
for (let item of array) {
helpEmbedMsg.addField(item, "text here", true);
}
// add the rest
helpEmbedMsg.addField("\u200B", "\u200B", false)
.addField(`Type \`${prefix}help <command>\` to learn more about a command and it's usage! `, `Example: \`${prefix}help ping\``, false)
.setTimestamp()
.setFooter(`Command triggered in ${message.channel.guild}`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.