Nested ForEach inside message fields

I’ve been trying to make a discord bot to make custom embeds. Essentially, I’m trying to parse out a giant string of text and create individual fields without having to waste time making multiple field adjustments. In order to do so, I’ve tried nest a forEach statement inside the field itself, but JS doesn’t seem to like that. I’m not quite sure how to cast this without using a variable inside the message call. Any ideas?

        message.channel.send({embed:{
            title:embtit[2],
            color:embtit[1],
            fields: [
                efields.forEach(function(elem, index, array) {
                    {
                        name:,
                        value:elem,
                        inline:ifields[index],
                    
                    }
                }
            ],
        }})

It’s not going to work because forEach method doesn’t return anything. It executes a function once for each element of array.

You’ll probably want to use map instead, which returns a new array.

You could do something like this:

fields: array.map( (current, index) => ({
    name: current.name,
    value: current.value
  })
}

Array.prototype.map()

1 Like

Thank you! That was helpful. My program now flows like it should without errors, but it’s not assigning the values into the fields like it should. They all just still show as undefined. But I know that the array is populated with information. I tested it. This is how I worked your code into my code:

  if (msg.startsWith(prefix + 'EMBED')) {
     var efields = [];
     var ifields = [];
     console.log("Embed Start...");
     embcont.forEach(function(elem, index, array) {
        if (index != 0) {
        ifields[index -1] = isInline(elem);
        efields[index - 1] = createEField(elem);
        }
        console.log("Arrays Created...");
        console.log(ifields);
        console.log(efields);

        message.channel.send({embed:{
            title:embtit[2],
            color:embtit[1],
            fields: efields.map( (current, index) => ({
                value: current.value,
                inline: ifields[index]
            }))
       
        }})
        console.log("Embed Finished!");
     });
     function isInline(e) {
        return e.startsWith("$I");
     };
     function createEField(e) {
        if(e.startsWith("$G")){
            return "'''" + e;
        } else return e;
     };

This is the embed that my code creates with the input that I entered:
image

It’s hard to say really without a context and without seeing the whole code that surrounds it. It’s all too abstract for me to figure out what is going on.

Which fields are undefined? What are the Roles ? What is the output of these console.logs?