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 });
}
CodePudding user response:
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.