'Getting duplicate results from DB
let absent_remove = result_database.filter(item => item.absentday != absent_days)
console.log("absent_remove", absent_remove);
I got this piece of code, I have multiple values in the database, I also joined the absent table with another table. This execution works properly, its meant to catch people who are absent on current day (today is friday = 5, so people absent on friday are removed from the array).
However, the remaining results are duplicated. As you can see the values are mostly all the same, but the absentday is causing for duplicates. I need it so that it only takes 1 one of the individual IDs
absent_remove [
{
id: 49,
absent_id: 1,
absentday: 3,
name: 'testing',
selected: 1,
slack_id: 'test',
absent: 0
},
{
id: 49,
absent_id: 1,
absentday: 2,
name: 'testing',
selected: 1,
slack_id: 'test',
absent: 0
},
{
id: 50,
absent_id: 2,
absentday: 1,
name: 'hello1',
selected: 0,
slack_id: 'hello1',
absent: 0
},
{
id: 50,
absent_id: 2,
absentday: 4,
name: 'hello1',
selected: 0,
slack_id: 'hello1',
absent: 0
}
]
It should become this
{
id: 49,
absent_id: 1,
absentday: 2,
name: 'testing',
selected: 1,
slack_id: 'test',
absent: 0
},
{
id: 50,
absent_id: 2,
absentday: 1,
name: 'hello1',
selected: 0,
slack_id: 'hello1',
absent: 0
},
SQL QUERY
db.connect(function (err) {
db.query("SELECT id, developers.absent_id, absentday, name, selected, slack_id, absent FROM developers, absent WHERE absent.absent_id=developers.absent_id",
(err, result, fields) => {
if (err) {
console.log(err);
} else {
result_database = Object.values(JSON.parse(JSON.stringify(result)));
console.log("Result from all Database Rows", result_database);
resultStatement();
}
})
})
Solution 1:[1]
const arrUniq = [...new Map(arr.map(v => [v.id, v])).values()]
Helped me out thanks to jsN00b redirection thread. This catches out the duplicate IDs.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Wake |