How can I get a unique array based on object property using underscore

前端 未结 8 1237
遇见更好的自我
遇见更好的自我 2020-12-29 18:30

I have an array of objects and I want to get a new array from it that is unique based only on a single property, is there a simple way to achieve this?

Eg.



        
8条回答
  •  庸人自扰
    2020-12-29 18:38

    unique array by id property with ES6:

    arr.filter((a, i) => arr.findIndex(b => b.id === a.id) === i);  // unique by id
    

    replace b.id === a.id with the relevant comparison for your case

提交回复
热议问题