0
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();
            }
        })
})
Wake
  • 73
  • 6
  • can you share your mysql query – Pattatharasu Nataraj May 06 '22 at 10:54
  • I'll just throw the full function lemme edit it – Wake May 06 '22 at 10:55
  • try using DISTINCT - SELECT DISTINCT id... – Pattatharasu Nataraj May 06 '22 at 10:58
  • Why not add absent_day to the query, i presume you want the absent per day? – Grumpy May 06 '22 at 10:59
  • DISTINCT doesn't work. And absent day is added to the query, its called absentday. Basically there are developers within the company who have different workdays, some are free on fridays or mondays, so with a secondary table and joining that with core, I can add numbers to the secondary table and pair that with the getDay(), which returns only numbers. So should It be friday (number 5), all developers with the value 5 should be excluded from the execution. – Wake May 06 '22 at 11:03
  • It appears that the SQL is sending in the duplicates. If you are not inclined to remove dupes at the source (ie, SQL) it can be done on the `result_database` array at javascript. Which column/s would be the key to identify uniqueness - simply `id` would do? If so, please [check if this](https://stackoverflow.com/q/2218999/13658816) helps. – jsN00b May 06 '22 at 11:23
  • Yeah that thread helped me out, got it sorted now thanks a lot. If anyone would be wondering I'll put the answer in the thread. – Wake May 06 '22 at 11:42

1 Answers1

0
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.

Wake
  • 73
  • 6