Javascript 从阵列中删除类似对象

Javascript 从阵列中删除类似对象,javascript,arrays,object,lodash,Javascript,Arrays,Object,Lodash,我试图从一个数组中删除objectst,该数组具有4个相同的值和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", Tit

我试图从一个数组中删除objectst,该数组具有4个相同的值和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"}
]
我曾尝试使用lodash
\u uniq
函数,但它只需要一个属性即可匹配:

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

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

使用.uniqBy,它允许自定义条件对数组进行uniqify操作。

使用.uniqBy,它允许自定义条件对数组进行uniqify操作。

可以通过两个嵌套循环来完成此操作,该循环将对数组本身进行迭代

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);
})
var newArray=[]//结果数组
//迭代
data.forEach(函数(项、索引){
var=false;
//检查其余元素,看看是否有匹配项
对于(var i=索引;i
您可以通过两个嵌套循环来完成此操作,这两个嵌套循环将对数组本身进行迭代

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);
})
var newArray=[]//结果数组
//迭代
data.forEach(函数(项、索引){
var=false;
//检查其余元素,看看是否有匹配项
对于(var i=索引;i
这可能会对您有所帮助,在这段代码中,我首先映射应该比较的所有属性,然后减少数组以仅获取唯一记录

 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”应该可以工作。

这可以帮助您,在这段代码中,我首先映射所有应比较的属性,然后减少数组以仅获取唯一的记录

 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”应该可以工作。

您可以使用普通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;

}
Array.prototype.allValuesSame=function(){
对于(var i=1;i

我为您做了一个测试。

您可以使用普通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;

}
Array.prototype.allValuesSame=function(){
对于(var i=1;i

我为你做了一个解释。

我在理解语法方面有点困难。我试过这个
\uqby($scope.data,“职务”、“部门”)仅根据第一个参数
作业标题
计算。有什么建议吗?@TietjeDK使用这里给出的好方法:我在理解语法方面有点困难。我试过这个
\uqby($scope.data,“职务”、“部门”)仅根据第一个参数
作业标题
计算。有什么建议吗?@ Tejjdk在这里使用了好的方法:可能的副本,请考虑接受可能的副本,请考虑接受答复。