2017-04-04 135 views
-1

我有兩個數組與對象,如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

回答

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

它按預期工作,謝謝! – User123

0

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

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