re: How to remove duplicate objects from an array (javascript) VIEW POST

VIEW PARENT COMMENT VIEW FULL DISCUSSION
 

wow, good solution.

The final solution with typos fixed.

function getUnique(arr){
let set = new Set();
return arr.map((v, index) => { 
   if(set.has(v.id)) {
       return false
   } else {
       set.add(v.id);
       return index;
   } 
  }).filter(e=>e).map(e=>arr[e]);


}
 

This was a great article. This helped me with my project. I was using faker to create some data but faker created duplicate names in an array of objects, but the client didn't want any duplicates.

Worked perfectly Thanks!!

Still trying to understand the code though.

Especially
.filter(e=>e).map(e=>arr[e]);

Gotta dig deeper with map and filter!

code of conduct - report abuse