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

前端 未结 8 1232
遇见更好的自我
遇见更好的自我 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:34

    A better and quick approach

    var table = [
      {
        a:1,
        b:2
      },
      {
        a:2,
        b:3
      },
      {
        a:1,
        b:4
      }
    ];
    
    let result = [...new Set(table.map(item => item.a))];
    document.write(JSON.stringify(result));
    

    Found here

提交回复
热议问题