#javascript #node.js #arrays #object #discord.js
#javascript #node.js #массивы #объект #discord.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)
Ответ №1:
Итак, просто разбейте код на две цепочки с 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}`);