如何比较两个对象数组的重复javascript

时间:2017-04-04 18:58:19

标签: javascript arrays

我有两个带有arr1和arr2等对象的数组,我想分开重复的项目和原始项目。我的意思是,从arr1开始,无论项目与arr2匹配,那些[来自arr1]的项目都被视为重复项目。如果它不匹配,则它是原始项目。

我做了以下程序,但是,它没有按预期工作

arr1 = [{name: "sunrise.jpg"},{name: "nature.jpg"},{name:"sunflower.jpg"}, {name: "sunset.jpg"}];

arr2 = [{
            "@type" : "Image",
            "objectTypeId" : "Image",
            "baseTypeId" : "document",
            "name" : "sunrise.jpg",
            "width" : "284",
            "height" : "177"
        }, {
            "@type" : "Image",
            "objectTypeId" : "Image",
            "baseTypeId" : "document",
            "name" : "Lao Tzu",
            "width" : "638",
            "height" : "960"
        }, {
            "@type" : "Image",
            "objectTypeId" : "Image",
            "baseTypeId" : "document",
            "name" : "nature.jpg",
            "width" : "300",
            "height" : "168"
        }, {
            "@type" : "Image",
            "objectTypeId" : "Image",
            "baseTypeId" : "document",
            "name" : "replay_12",
            "width" : "500",
            "height" : "717"
        }, {
            "@type" : "Image",
            "objectTypeId" : "Image",
            "baseTypeId" : "document",
            "name" : "sunflower.jpg",
            "width" : "300",
            "height" : "168"
        }
    ];

var originalItems = [];
var diff = function(arr1, arr2) {
    var dupes = [];

    for(var i in arr1) {   
    //console.log(i);
         for(var j in arr2){
           if(arr1[i].name === arr2[j].name){
             dupes.push(arr2[j]);
           } else {
            originalItems.push(arr2[j]);
           }
         }       
    }
    return dupes;
}   

var sd = diff(arr1, arr2);

console.log(sd);
console.log(originalItems);

在这里,原始项目有'sunset.jpg',其余的被移到'dupes'。

我在这里遗漏了什么,有任何线索,请?

3 个答案:

答案 0 :(得分:1)

您可以使用Array#someArray#every来确定arr1是否包含给定元素,并使用Array#filter对其进行过滤。

var arr1 = [{name: "sunrise.jpg"},{name: "nature.jpg"},{name:"sunflower.jpg"}, {name: "sunset.jpg"}],
    arr2 = [{"@type":"Image","objectTypeId":"Image","baseTypeId":"document","name":"sunrise.jpg","width":"284","height":"177"},{"@type":"Image","objectTypeId":"Image","baseTypeId":"document","name":"Lao Tzu","width":"638","height":"960"},{"@type":"Image","objectTypeId":"Image","baseTypeId":"document","name":"nature.jpg","width":"300","height":"168"},{"@type":"Image","objectTypeId":"Image","baseTypeId":"document","name":"replay_12","width":"500","height":"717"},{"@type":"Image","objectTypeId":"Image","baseTypeId":"document","name":"sunflower.jpg","width":"300","height":"168"}], 
    dupes = arr2.filter(v => arr1.some(c => c.name == v.name)),
    originals =  arr2.filter(v => arr1.every(c => c.name != v.name));
    
    console.log(dupes);
    console.log(originals);

答案 1 :(得分:0)

函数checkDuplicates将返回对象{},其中包含两个数组,第一个重复,第二个非重复。



function checkDuplicates(array, compareWith) {
   var nonDuplicates = [];
   var duplicates = [];
   
  for (i = 0; i < array.length; i++) {
    var element = array[i];
    var isDuplicate = false;
    
    for (j = 0; j < compareWith.length; j++) {
    
      var comparedElement = compareWith[j];
      
      if (element.name === comparedElement.name) {
        isDuplicate = true;
        duplicates.push(comparedElement);
      }
      
    }
    
    if (!isDuplicate) {
      nonDuplicates.push(element);
    }
  
  }
  //ES6 would be
  //return  { duplicates, nonDuplicates };  
  return {duplicates: duplicates, nonDuplicates: nonDuplicates };


}

var arr1 = [{name: "sunrise.jpg"},{name: "nature.jpg"},{name:"sunflower.jpg"}, {name: "sunset.jpg"}];

var arr2 = [{
            "@type" : "Image",
            "objectTypeId" : "Image",
            "baseTypeId" : "document",
            "name" : "sunrise.jpg",
            "width" : "284",
            "height" : "177"
        }, {
            "@type" : "Image",
            "objectTypeId" : "Image",
            "baseTypeId" : "document",
            "name" : "Lao Tzu",
            "width" : "638",
            "height" : "960"
        }, {
            "@type" : "Image",
            "objectTypeId" : "Image",
            "baseTypeId" : "document",
            "name" : "nature.jpg",
            "width" : "300",
            "height" : "168"
        }, {
            "@type" : "Image",
            "objectTypeId" : "Image",
            "baseTypeId" : "document",
            "name" : "replay_12",
            "width" : "500",
            "height" : "717"
        }, {
            "@type" : "Image",
            "objectTypeId" : "Image",
            "baseTypeId" : "document",
            "name" : "sunflower.jpg",
            "width" : "300",
            "height" : "168"
        }
    ];
    
console.log(checkDuplicates(arr1, arr2));
&#13;
&#13;
&#13;

答案 2 :(得分:0)

{{1}}