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

问题描述:

我有两个数组与对象,如arr1和arr2,我想分开重复的项目和原始项目。我的意思是,从arr1开始,无论项目是否与arr2匹配,那么这些项目[来自arr1]都被视为重复项目。如果它不匹配,那么它就是原始物品。如何比较两个对象数组中的重复javascript

我做了以下程序,但它很未按预期

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”和其他被移动到“愚弄”。

我错过了这里的任何线索,请问?

+1

你有什么期望从上面的例子的结果? –

+0

我坚持复制和粘贴你的代码在jsfiddle中,结果不是你说的。我得到愚蠢=日出,自然,向日葵;其余的在原件。我什么也没做,只是复制并粘贴 – lascort

+0

@Kinduser实际上,你是对的,它不工作,“originalItems”数组包含所有项目。但结果绝对不是问题所在。但是,是的,我的坏 – lascort

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

它按预期工作,谢谢! – User123

功能checkDuplicates将返回对象{}带有两个阵列,第一重复和第二nonduplicates。

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));

var arr1 = ...; 
var arr2 = ...; 

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

    arr2.forEach(function(o2) {    // for each object o2 in arr2 
     var test = arr1.some(function(o1) { // check if there is some object from arr1 that has the same name 
      return o1.name === o2.name; 
     }); 
     if(test)        // if there is 
      dupes.push(o2);     // then push it into the dupes array 
     else         // if not 
      originalItems.push(o2);   // then it is an original 
    }); 

    return {         // return like this is much organized 
     dupes: dupes, 
     originalItems: originalItems 
    }; 
} 

var res = diff(arr1, arr2);     // the result is an object containing two arrays: the dupes one and the originalItems one 

console.log(res.dupes); 
console.log(res.originalItems);