javascript - Filter an array of objects to only have the last occurrence of that object with the same certain property -
how filter array of objects have last occurrence of object same prop of foo? i.e
given:
[ {foo:1,id:1}, {foo:1,id:2}, {foo:2,id:3} ] i want back:
[ {foo:1,id:2}, {foo:2,id:3} i'm using es6 can start here:
 this.data.filter((item,index,self)=> {      return self.findindex(o => o.foo === item.foo);     }) 
var index = {}; var data = [{foo:1,id:1},{foo:1,id:2},{foo:2,id:3}];  data.foreach(item => index[item.foo] = item);  index[1]; // {foo:1,id:2}  object.values(index) // [{foo:1,id:2},{foo:2,id:3}] 
Comments
Post a Comment