Recently, I had to remove duplicates from an array of objects that I retrieved from MongoDB. Here is how I made it work:
let users = [{_id:"abc",name:"Bob"},{_id:"xyz",name:"Al"},{_id:"abc",name:"Bob"}];
let uniqueUsers = [...new Set(users.map(x => JSON.stringify(x)))].map(y => JSON.parse(y));
console.log(users)
console.log(uniqueUsers) //expected output : [{_id:"abc",name:"Bob"},{_id:"xyz",name:"Al"}]
But I am wondering, is it an efficient way of doing it? Can it be done more elegantly? I figured that using some of the new features of ES6 would be a good way. What do you think ?
Edit : Since my objects are MongoDB documents, their ID is unique.