Search code examples
javascriptnode.jsdiscord.jstrello

Discord.js v13 | limit results given by forEach. | Trello-node-api


so I'm trying to limit results with they're greater than 5. It works sorta but it keeps repeating the results.

Here's my code:

trelloNode.board.searchCards("tOmOukAB").then(function(response) {
                //console.log(response) 
                
                response.forEach(element => {
                  if (element.name.includes(id)) {
                    if (element.idList === myListId) {
                      i++
                      console.log(i)
                      console.log(element.url)  
  let date = new Date(element.due) // states join date
                      console.log(date);
     if (i > 5) {
for(var b = 0; b < 5; b++) {
       embed.addField(`Ban ${b}`, `Due: [${new Intl.DateTimeFormat('en-US', { dateStyle: 'full', timeStyle: 'long' }).format(date)}](${element.url})\n${element.desc}`, true);                   
     }
                
                      } else if (i < 5) {
              embed.addField(`Ban ${i}`, `Due: [${new Intl.DateTimeFormat('en-US', { dateStyle: 'full', timeStyle: 'long' }).format(date)}](${element.url})\n${element.desc}`, true); 
         
                     }
                    }
                  }
                })
                if (i == 0) {
                  const embed2 = new MessageEmbed()

                    .setColor("#FF0505")

                    .setDescription(`**${username2}** isn't banned!`)

                  return interaction.reply({ embeds: [embed2] })
                } else if (i > 0) {
  let re = i - 5
            if (re >= 1){ embed.setFooter({ text: `${re} more ban(s)...`});
                    }

                  return interaction.reply({ embeds: [embed] })
                }

It's hard to explain but what I'm trying to do is just check if the response length is greater that 5 and if it is then it only adds 5 fields and at the bottom it'll add the remaining. Please comment if you have any questions.


Solution

  • I found a way to do it.

    trelloNode.board.searchCards("tOmOukAB").then(function(response) {
                    let elemental = []
                  
      for (var a = 0; a < response.length; a++) {
              const element = response[a];
            if (element.name.includes(id)) {
              if (element.idList === myListId) {
                          i++
                        elemental.push({url: String(element.url), due: element.due, desc: String(element.desc)})  
                         
                        }
                      }
                    }
             if (i > 5) {
    
    var b = 0;
    elemental.slice(0, 5).forEach(element => {  
     b++
           let date = new Date(element.due);
           console.log(element.url); 
                            embed.addField(`Ban ${b}`, `Due: [${new Intl.DateTimeFormat('en-US', { dateStyle: 'full', timeStyle: 'long' }).format(date)}](${element.url})\n${element.desc}`, true);
                      })
                    
                      let re = i - 5
                      if (re >= 1) { embed.setFooter({ text: `${re} more ban(s)...` });
                      }
                      
                    }
            if (i < 5) {
    
    var c = 0;
           elemental.forEach(element => {  
             c++
     
           let date = new Date(element.due);
           console.log(element.url); 
                            embed.addField(`Ban ${c}`, `Due: [${new Intl.DateTimeFormat('en-US', { dateStyle: 'full', timeStyle: 'long' }).format(date)}](${element.url})\n${element.desc}`, true);
                      })
                    
                    }