Remove duplicates in an object array Javascript

前端 未结 8 1117
误落风尘
误落风尘 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:26

    The following will work:

    var a = [{x:1,y:2}, {x:3,y:4}, {x:5,y:6}, {x:1,y:2}];
    
    var b = _.uniq(a, function(v) { 
        return v.x && v.y;
    })
    
    console.log(b);  // [ { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 } ]
    

提交回复
热议问题