0

I have an object in which I want to be able to dynamically add at least one property. The amount of properties depends on the items inside of an array that I have. Here's the object I'm referencing:

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}`);

So, basically, if the length of the array is 4, I want to add the following:

.addField(array[i], "text here", true)

four times, but in a specific place in the object (the order counts), right below this line:

.addField("📋 Here's a list of all my commands:", "\u200B", false)

1 Answer 1

3

So, just break up the code into two chains with a for loop between them:

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}`);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.