Get list of duplicate objects in an array of objects

前端 未结 7 859
你的背包
你的背包 2020-12-14 22:11

I am trying to get duplicate objects within an array of objects. Let\'s say the object is like below.

values = [
  { id: 10, name: \'someName1\' },
  { id: 1         


        
相关标签:
7条回答
  • 2020-12-14 22:29

    With lodash you can solve this with filter and countBy for complexity of O(n):

    const data = [{ id: 10,name: 'someName1' }, { id: 10,name: 'someName2' }, { id: 11,name: 'someName3' }, { id: 12,name: 'someName4' } ]
    
    const counts = _.countBy(data, 'id')
    console.log(_.filter(data, x => counts[x.id] > 1))
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

    You could do the same with ES6 like so:

    const data = [{ id: 10,name: 'someName1' }, { id: 10,name: 'someName2' }, { id: 11,name: 'someName3' }, { id: 12,name: 'someName4' } ]
    
    const countBy = (d, id) => d.reduce((r,{id},i,a) => (r[id] = a.filter(x => x.id == id).length, r),{})
    const counts = countBy(data, 'id')
    console.log(data.filter(x => [x.id] > 1))

    0 讨论(0)
  • 2020-12-14 22:31

    With lodash you can use _.groupBy() to group elements by their id. Than _.filter() out groups that have less than two members, and _.flatten() the results:

    const values = [{id: 10, name: 'someName1'}, {id: 10, name: 'someName2'}, {id: 11, name:'someName3'}, {id: 12, name: 'someName4'}];
    
    const result = _.flow([
      arr => _.groupBy(arr, 'id'), // group elements by id
      g => _.filter(g, o => o.length > 1), // remove groups that have less than two members
      _.flatten // flatten the results to a single array
    ])(values);
    
    console.log(result);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

    0 讨论(0)
  • 2020-12-14 22:39

    You can use an array to store unique elements and use filter on values to only return duplicates.

    const unique = []
    
    const duplicates = values.filter(o => {
    
       if(unique.find(i => i.id === o.id && i.name === o.name)) {
         return true
       }
    
       unique.push(o)
       return false;
    })
    
    0 讨论(0)
  • 2020-12-14 22:41

    Let's say you have:

    arr = [
        { id:10, name: 'someName1' },
        { id:10, name: 'someName2' },
        { id:11, name: 'someName3' },
        { id:12, name: 'someName4' }
    ]
    

    So, to get unique items:

    unique = arr
         .map(e => e['id'])
         .map((e, i, final) => final.indexOf(e) === i && i)
         .filter(obj=> arr[obj])
         .map(e => arr[e]);
    

    Then, result will be

    unique = [
         { id:10, name: 'someName1' },
         { id:11, name: 'someName3' },
         { id:12, name: 'someName4' }
    ]
    

    And, to get duplicate ids:

    duplicateIds = arr
         .map(e => e['id'])
         .map((e, i, final) => final.indexOf(e) !== i && i)
         .filter(obj=> arr[obj])
         .map(e => arr[e]["id"])
    

    List of IDs will be

    duplicateIds = [10]
    

    Thus, to get duplicates objects:

    duplicate = arr.filter(obj=> dublicateIds.includes(obj.id));
    

    Now you have it:

    duplicate = [
        { id:10, name: 'someName1' },
        { id:10, name: 'someName2' }
    ]
    

    Thanks https://reactgo.com/removeduplicateobjects/

    0 讨论(0)
  • 2020-12-14 22:44

    You haven't clarified whether two objects with different ids, but the same "name" count as a duplicate. I will assume those do not count as a duplicate; in other words, only objects with the same id will count as duplicate.

    let ids = {};
    let dups = [];
    
    values.forEach((val)=> {
      if (ids[val.id]) {
        // we have already found this same id
        dups.push(val)
      } else {
        ids[val.id] = true;
      }
    })
    return dups;
    
    0 讨论(0)
  • 2020-12-14 22:47

    Try this

    function checkDuplicateInObject(propertyName, inputArray) {
      var seenDuplicate = false,
      testObject = {};
    
      inputArray.map(function(item) {
      var itemPropertyName = item[propertyName];    
      if (itemPropertyName in testObject) {
      testObject[itemPropertyName].duplicate = true;
      item.duplicate = true;
      seenDuplicate = true;
     }
     else {
       testObject[itemPropertyName] = item;
       delete item.duplicate;
     }
    });
    
     return seenDuplicate;
    }
    

    referred from : http://www.competa.com/blog/lets-find-duplicate-property-values-in-an-array-of-objects-in-javascript/

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