Remove duplicates in an object array Javascript

前端 未结 8 1105
误落风尘
误落风尘 2020-12-01 16:03

I have an array of objects

list = [{x:1,y:2}, {x:3,y:4}, {x:5,y:6}, {x:1,y:2}]

And I\'m looking for an efficient way (if possible O(

8条回答
  •  爱一瞬间的悲伤
    2020-12-01 16:22

    One liners for ES6+

    If you want to find uniq by x and y:

    arr.filter((v,i,a)=>a.findIndex(t=>(t.x === v.x && t.y===v.y))===i)
    

    If you want to find uniques by all properties:

    arr.filter((v,i,a)=>a.findIndex(t=>(JSON.stringify(t) === JSON.stringify(v)))===i)
    

提交回复
热议问题