writingdeveloper
writingdeveloper

Reputation: 1076

How to remove duplicate data in JavaScript RowDataPacket Array

I got a data from my MySQL Server with Nodejs and I want to remove duplicate data in this array, but the structure of the array looks weird and I tried various method to solve this problem but, it's useless.

Code

router.get(`/find-users/:queryString`, function(req, res, next) {
  let queryString = req.params.queryString;
  db.query(
    `SELECT userId from project WHERE keyword like "%${queryString}%"`,
    function(error, data) {
      // console.log(Array.from(new Set(data)));
      console.log(data);
    }
  );
  res.json(data);
});

Result Data

[ RowDataPacket { userId: 'arpitjindal97' },
  RowDataPacket { userId: 'cs01' },
  RowDataPacket { userId: 'samuelcolvin' },
  RowDataPacket { userId: 'Jyrno42' },
  RowDataPacket { userId: 'LemonyDesign' },
  RowDataPacket { userId: 'thruthesky' },
  RowDataPacket { userId: 'thruthesky' },
  RowDataPacket { userId: 'thruthesky' },
  RowDataPacket { userId: 'thruthesky' },
  RowDataPacket { userId: 'apollographql' },
  RowDataPacket { userId: 'samuelcolvin' },
  RowDataPacket { userId: 'Jyrno42' },
  RowDataPacket { userId: 'Jyrno42' },
  RowDataPacket { userId: 'thruthesky' }
}

I tried console.log(Array.from(new Set(data))); but the result is same. How can I solve this problem?

Upvotes: 0

Views: 375

Answers (1)

wang arthur
wang arthur

Reputation: 26

just try distinct to remove duplicate data

router.get(`/find-users/:queryString`, function(req, res, next) {
  let queryString = req.params.queryString;
  db.query(
    `SELECT distinct userId from project WHERE keyword like "%${queryString}%"`,
    function(error, data) {
      // console.log(Array.from(new Set(data)));
      console.log(data);
    }
  );
  res.json(data);
});

Upvotes: 1

Related Questions