从数组中删除类似的对象

时间:2016-04-28 08:58:16

标签: javascript arrays object lodash

我正在尝试从具有4个相同值和1个唯一值的数组中删除objectst。

快速谷歌搜索提供了很多选项,可以从数组中删除相同的对象,但不包含类似的对象。

数组示例:

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"}
]

我尝试过使用lodash _uniq函数,但只需要匹配一个属性:

var non_duplidated_data = _.uniq(data, 'Name'); 

除日期外,所有值均相同。如何根据4个属性删除相同的对象?

4 个答案:

答案 0 :(得分:1)

使用.uniqBy https://lodash.com/docs#uniqBy,它允许自定义条件来统一你的数组。

答案 1 :(得分:1)

你可以通过两个嵌套的循环来完成这个循环,这个循环将迭代数组。

var newArray = [];   //Result array

//iterate
data.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 found
    if (!found)
       newArray.push(item);
})

答案 2 :(得分:1)

这可以帮到你,在这段代码中我首先映射应该比较的所有属性,然后我减少数组以便只获取唯一记录。

 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;

});

结果将是:

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

然后你需要定义你想要保留的日期(第一个,最后一个,哪个?)并再次找到记录,为此,一个简单的'forEach'应该有效。

答案 3 :(得分:1)

您可以使用普通的Javascript解决此问题。 只需检查每个属性的每个对象:

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

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 array
    if (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;

}

我为你做了fiddle