Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

453
Views
how to send this array as one embed message in discord.js (javascript)

I want to send this data as one embed message and I don't know how many of these we have.

I tried to do like this :

            let list = hk;
            var id = "";
            var username = "";
            var identifier = ""
            for (var i = 0; i < list.length; i++) {
                id += list[i].id + '\n';
                username += list[i].user_name + '\n';
                identifier += list[i].identifier + '\n';

            }
                const pListEmbed = new Discord.MessageEmbed()
                    .setColor('#03fc41')
                    .setTitle('Connected')
                    .setDescription(`Total : ${list.length}`)
                    .setThumbnail(config.logo)
                    .addFields({ name: 'ID', value: id, inline: true }, { name: 'Name', value: username, inline: true }, { name: 'Identifier', value: identifier, inline: true },

                    )
                    .setTimestamp(new Date())
                    .setFooter('Used by: ' + message.author.tag, `${config.SERVER_LOGO}`);

                message.channel.send(pListEmbed);
        });

but it sends several separate embed messages, each containing the data and hk is this array that we don't know how many of the data we have

array :

        [
        {
      id: '46892319372',
      user_name: 'testerOne',
      identifier: '20202'
    }
]
[
    {
      id: '15243879678',
      user_name: 'testerTwo',
      identifier: '20201'
    }
]
[
    {
      id: '02857428679',
      user_name: 'testerThree',
      identifier: '20203'
    }
]
[
    {
      id: '65284759703',
      user_name: 'testerFour',
      identifier: '20204'
    }
]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you can map the array into the fields like this:

separate fields

.addFields(
  array.flatMap(user => [
    { name: 'ID', value: user.id, inline: true },
    { name: 'Name', value: user.user_name, inline: true },
    { name: 'Identifier', value: user.identifier, inline: true }
  ])
)

single fields

.addFields(
  array.flatMap(user => [
    { name: 'User', value: `${id + username + identifier}`, inline: true },
  ])
)

Why flatMap()?

flatMap() is an inbuilt function in JavaScript which is used to flatten the input array element into a new array. This method first of all map every element with the help of mapping function, then flattens the input array element into a new array.

about 4 years ago · Juan Pablo Isaza Report

0

Simply use .forEach, that will loop over every single element and use the "addFields" method ->

// .setThumbnail()..
list.forEach(user => pListEmbed.addFields(
  { name: 'ID', value: user.id, inline: true },
  { name: 'Name', value: user.user_name, inline: true },
  { name: 'Identifier', value: user.identifier, inline: true }
))

message.reply({ embeds : [pListEmbed] })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!