[removed] Remove duplicates of objects sharing same property value

后端 未结 12 1469
执念已碎
执念已碎 2020-11-30 06:00

I have an array of objects that I would like to trim down based on a specific key:value pair. I want to create an array that includes only one object per this s

相关标签:
12条回答
  • 2020-11-30 06:05

    Simple solution although not the most performant:

    var unique = [];
    duplicates.forEach(function(d) {
        var found = false;
        unique.forEach(function(u) {
            if(u.key == d.key) {
                found = true;
            }
        });
        if(!found) {
            unique.push(d);
        }
    });
    
    0 讨论(0)
  • 2020-11-30 06:05
    for (let i = 0; i < arrayWithDuplicates.length; i++) {
         for (let j = i + 1; j < arrayWithDuplicates.length; j++) {
           if (arrayWithDuplicates[i].name === students[j].name) {
              arrayWithDuplicates.splice(i, 1);
           }
         }
        }
    
    this will work perfectly...and this will delete first repeated array.
    To delete last repeated array we only have to change
     arrayWithDuplicates.splice(i, 1) ; into
     arrayWithDuplicates.splice(j, 1);
    
    0 讨论(0)
  • 2020-11-30 06:12

    You can use lodash to remove duplicate objects:

     import * as _ from 'lodash';
      _.uniqBy(data, 'id');
    

    Here 'id' is your unique identifier

    0 讨论(0)
  • 2020-11-30 06:12

    using lodash you can filter it out easily

    the first parameter will be your array and second will be your field with duplicates

    _.uniqBy(arrayWithDuplicates, 'color')
    

    it will return an array with unique value

    0 讨论(0)
  • 2020-11-30 06:18

    I don't think there's a built-in function in Angular, but it isn't hard to create one:

    function removeDuplicates(originalArray, objKey) {
      var trimmedArray = [];
      var values = [];
      var value;
    
      for(var i = 0; i < originalArray.length; i++) {
        value = originalArray[i][objKey];
    
        if(values.indexOf(value) === -1) {
          trimmedArray.push(originalArray[i]);
          values.push(value);
        }
      }
    
      return trimmedArray;
    
    }
    

    Usage:

    removeDuplicates(arrayWithDuplicates, 'size');
    

    Returns:

    [
        {
            "color": "red",
            "size": "small"
        },
        {
            "color": "blue",
            "size": "medium"
        },
        {
            "color": "red",
            "size": "large"
        }
    ]
    

    And

    removeDuplicates(arrayWithDuplicates, 'color');
    

    Returns:

    [
        {
            "color": "red",
            "size": "small"
        },
        {
            "color": "green",
            "size": "small"
        },
        {
            "color": "blue",
            "size": "medium"
        }
    ]
    
    0 讨论(0)
  • 2020-11-30 06:19

    You can use underscore for this:

    //by size:
    var uSize = _.uniqBy(arrayWithDuplicates, function(p){ return p.size; });
    
    //by custom.price;
    var uPrice = _.uniqBy(arrayWithDuplicates, function(p){ return p.custom.price; });
    
    0 讨论(0)
提交回复
热议问题