Remove duplicates in an object array Javascript

前端 未结 8 1084
误落风尘
误落风尘 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条回答
  •  -上瘾入骨i
    2020-12-01 16:17

    Plain javascript (ES2015), using Set

    const list = [{ x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }, { x: 1, y: 2 }];
    
    const uniq = new Set(list.map(e => JSON.stringify(e)));
    
    const res = Array.from(uniq).map(e => JSON.parse(e));
    
    document.write(JSON.stringify(res));

提交回复
热议问题