Skip to content Skip to sidebar Skip to footer

Remove Similar Objects From Array

I'm trying to remove objectst from an array which have 4 identical values and 1 unique. A quick google search gives a lot of options for removing identical objects from an array,

Solution 1:

You can solve this using plain Javascript. Just check each object for each property:

Array.prototype.allValuesSame = function() {
    for(var i = 1; i < this.length; i++) {
        if(this[i] !== this[0])
            returnfalse;
    }
    returntrue;
}

var data = [
   {Date: "2016-04-27T09:03:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"},
   {Date: "2016-04-27T08:07:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"},
   {Date: "2016-04-27T10:23:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"}
]

var tmpPropValues = {};
for (var property in data[0]) {
    tmpPropValues[property] = [];

    for(var i = 0; i < data.length; i++) {
        var obj = data[i];

    // store temporary in arrayif (obj.hasOwnProperty(property)) {
      var value = obj[property];

      tmpPropValues[property].push(value);
    }
  }

    if(tmpPropValues[property].allValuesSame()) {
      delete tmpPropValues[property];
  }
}

for(var prop in tmpPropValues) {
    var tmp = document.getElementById("result").innerHTML;
  document.getElementById("result").innerHTML = tmp+" "+prop;

}

I made a fiddle for you.

Solution 2:

Use .uniqBy https://lodash.com/docs#uniqBy, it allows custom criteria to uniqify your array.

Solution 3:

You can do this by two nested loops which will iterate the array against itself.

var newArray = [];   //Result array//iteratedata.forEach(function(item, index){

    var found = false; 

    //Go through the rest of elements and see if any matches  for (var i = index; i < data.length, i++) {
        if (item.name == data[i].name && item.title == data[i].title) // add more fields here
            found = true;      
    }   

    //Add to the result array if no duplicates foundif (!found)
       newArray.push(item);
})

Solution 4:

This could help you, in this code i start by mapping all properties that should be compared, then i reduce the array in order to get only the unique records.

var data = [
       {Date: "2016-04-27T09:03:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"},
       {Date: "2016-04-27T08:07:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"},
       {Date: "2016-04-27T10:23:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"}
    ];

var uniqueRecord = data.map(function(item){
    return {Name:item.Name,Title:item.Title,Department: item.Department,Company:item.Company};
}).reduce(function(acc,curr){
    if(!Array.isArray(acc)){
        var copyOfAcc = JSON.parse(JSON.stringify(acc));
        acc = [];
        acc.push(copyOfAcc);
    }
    return (acc.indexOf(curr) > -1) ? acc.push(curr) : acc;

});

The result would be:

[ { Name: 'Tito',
    Title: 'Developer',
    Department: 'IT',
    Company: 'XXX' } ]

Then you would need to define which date you want to keep (first, last, which?) and find again the records, for this a simple 'forEach' should work.

Post a Comment for "Remove Similar Objects From Array"