Reputation: 15
So I have have an array of names let namelist = ['name1', 'name2', 'name3', 'name4', 'name5'];
and i have a for loop that when a user uses the command $list ame
it sorts through the array and grabs any element that has 'ame' in it and pushes them into a new array temparray
.
My question is how can I make an embed that will grab temparray.length and make a new field for each string in the array?
I've tried using a for loop and a do...while() loop but I can't seem to figure out how to access the embed so i can use addField inside a loop. Is it possible to do that and/or is there a better way I can add a new line for each array element? This is the code I have for the embed without the loops. I want to get rid of those .addFields and put them in a loop for automation
if(typeof args[0] === 'string' && args[0].length >= 3){
let embed = new Discord.RichEmbed()
.setAuthor("Names containing " + "'" + `${args[0]}` + "'" )
.addField(temparray[0], `this is ${temparray[0}` )
.addField(temparray[1], `this is ${temparray[1}`)
.addField(temparray[2], `this is ${temparray[2}`)
.addField(temparray[3], `this is ${temparray[3}`)
.addField(temparray[4], `this is ${temparray[4}`)
.setColor("#92BA2F")
.setThumbnail(bot.user.avatarURL)
.setTimestamp(Date.now())
message.channel.send(embed);
}
Upvotes: 0
Views: 5768
Reputation: 217
Simple, just .addField()
on the embed variable in the loop.
let embed = new Discord.RichEmbed()
.setAuthor("Names containing " + "'" + `${args[0]}` + "'" )
.setColor("#92BA2F")
.setThumbnail(bot.user.avatarURL)
.setTimestamp(Date.now());
temparray.forEach(entry => {
embed.addField(entry, 'looped field');
});
message.channel.send(embed);
You don't need to use the length of your array for this, but if you wanted to, you can use for (let i = 0; i < temparray.length; i++)
.
Upvotes: 3