Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
Pushing to to array with multiple values using node js - discord js

So I have an empty array:

        const logsEmbed = {
            color: '15c9c9',
            title: '',
            fields: [],
            timestamp: new Date()
        }

The data I want to receive I want to look something like:

{
  name: headline1,
  value: value1 \n value2 \n value3,
},
{
  name: headline2,
  value: value1 \n value2 \n value3,
}

What i am getting:

{
  name: headline1,
  value: value1,
},
{
  name: headline1,
  value: value2,
}

Basicly I want to get 1 unique headline with all the values under it, and then go to the next headline and get all the values there

The data in the database looks like:

1|Headline1|Value1|2022-04-22 18:05:52.473 +00:00|2022-04-22 18:05:52.473 +00:00
2|Headline1|Value2|2022-04-22 18:15:40.061 +00:00|2022-04-22 18:15:40.061 +00:00
3|Headline1|Value3|2022-04-22 18:16:45.313 +00:00|2022-04-22 18:16:45.313 +00:00
4|Headline2|Value4|2022-04-22 18:19:13.985 +00:00|2022-04-22 18:19:13.985 +00:00
5|Headline2|Value5|2022-04-22 18:19:36.230 +00:00|2022-04-22 18:19:36.230 +00:00

Currently This array I am pushing some values to through a for loop from data in a database using sequelize, ofc that is not getting me where I want to get but.. I hope some can help me on my way

const raids = await logs_table.findAll({ attributes: ['raid'], group: ['raid'] });
        for(const raid of raids)
        {
            const logs = await logs_table.findAll({ attributes: ['raid', 'link' ], where: {raid: raid.raid }});
            for(const log of logs)
            {
                logsEmbed.fields.push({name: log.raid, value: log.link, inline: false });
            }
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I think this is what you are looking for:

const raids = await logs_table.findAll({
  attributes: ['raid'],
  group: ['raid'],
});
for (const raid of raids) {
  const logs = await logs_table.findAll({
    attributes: ['raid', 'link'],
    where: { raid: raid.raid },
  });
  logs.forEach((log) => {
    const matchIndex = logsEmbed.fields.findIndex(
      (field) => field.name === log.raid
    );
    if (matchIndex < 0)
      logsEmbed.fields.push({ name: log.raid, value: log.link });
    else logsEmbed.fields[matchIndex].value += ` \n ${log.link}`;
  });
}

Note that performing the DB request (logs_table.findAll) twice is probably a mistake, you should be able to retrieve all the data in one single request.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda