Filtering duplicate hashes from array of hashes – Javascript

  • A+
Category:Languages

I have an array of hashes, like this:

[{id: "4bf58dd8d48988d110941735", name: "italy"}  {id: "4bf58dd8d48988d1c6941735", name: "skandi"}  {id: "4bf58dd8d48988d147941735", name: "diner"}  {id: "4bf58dd8d48988d110941735", name: "italy"}  {id: "4bf58dd8d48988d1c4941735", name: "resto"}  {id: "4bf58dd8d48988d14a941735", name: "vietnam"}  {id: "4bf58dd8d48988d1ce941735", name: "fish"}  {id: "4bf58dd8d48988d1c4941735", name: "resto"}  {id: "4bf58dd8d48988d1c4941735", name: "resto"}] 

I want to throw out duplicate hashes. Set doesn't work because hashes are unique objects.

I feel stuck and need a kick to think. Please advise!

 


You can use reduce too

var data= [{id: "4bf58dd8d48988d110941735", name: "italy"},  {id: "4bf58dd8d48988d1c6941735", name: "skandi"},  {id: "4bf58dd8d48988d147941735", name: "diner"},  {id: "4bf58dd8d48988d110941735", name: "italy"},  {id: "4bf58dd8d48988d1c4941735", name: "resto"},  {id: "4bf58dd8d48988d14a941735", name: "vietnam"},  {id: "4bf58dd8d48988d1ce941735", name: "fish"},  {id: "4bf58dd8d48988d1c4941735", name: "resto"},  {id: "4bf58dd8d48988d1c4941735", name: "resto"}]   var result= data.reduce((current,next)=>{      if(!current.some(a=> a.name === next.name)){     current.push(next);   }   return current;  },[])  console.log(result);

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: