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?{embed:{
            fields: [
                efields.forEach(function(elem, index, array) {

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: (current, index) => ({
    value: current.value

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...");
            fields: (current, index) => ({
                value: current.value,
                inline: ifields[index]
        console.log("Embed Finished!");
     function isInline(e) {
        return e.startsWith("$I");
     function createEField(e) {
            return "'''" + e;
        } else return e;

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

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?