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

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

    You can use the _.uniqBy function

    var array = [ { id: 1, name: 'bob' }, { id: 2, name: 'bill' }, { id: 1, name: 'bill' },{ id: 2, name: 'bill' } ];
    
    var filteredArray = _.uniqBy(array,function(x){ return x.id && x.name;});
    
    console.log(filteredArray)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>

    In the above example, filtering is based on the uniqueness of combination of properties id & name.

    if you have multiple properties for an object. then to find unique array of objects based on specific properties, you could follow this method of combining properties inside _.uniqBy() method.

    0 讨论(0)
  • 2020-12-29 18:32

    In case you need pure JavaScript solution:

    var uniqueProperties = {};
    
    var notUniqueArray = [ { id: 1, name: 'bob' }, { id: 1, name: 'bill' }, { id: 1, name: 'bill' } ];
    
    
    for(var object in notUniqueArray){
       uniqueProperties[notUniqueArray[object]['name']] = notUniqueArray[object]['id'];
    }
    
    var uniqiueArray = [];
    
    for(var uniqueName in uniqueProperties){
       uniqiueArray.push(
         {id:uniqueProperties[uniqueName],name:uniqueName});
    }
    
    //uniqiueArray
    
    0 讨论(0)
  • 2020-12-29 18:34

    If you prefer to do things yourself without Lodash, and without getting verbose, try this uniq filter with optional uniq by property:

    const uniqFilterAccordingToProp = function (prop) {
        if (prop)
            return (ele, i, arr) => arr.map(ele => ele[prop]).indexOf(ele[prop]) === i
        else
            return (ele, i, arr) => arr.indexOf(ele) === i
    }
    

    Then, use it like this:

    const obj = [ { id: 1, name: 'bob' }, { id: 1, name: 'bill' }, { id: 1, name: 'bill' } ]
    obj.filter(uniqFilterAccordingToProp('abc'))
    

    Or for plain arrays, just omit the parameter, while remembering to invoke:

    [1,1,2].filter(uniqFilterAccordingToProp())

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-29 18:35

    If you want to check all the properties then lodash 4 comes with _.uniqWith(sourceArray, _.isEqual)

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题