2016-05-02 10 views
1

Ich versuche, doppelte Objekte im Array nach Objektwertnamen zu filtern und in neues Array namens finalResult einzufügen.doppelte Objekte nach Wertnamen filtern und in neues Array einfügen

Grundsätzlich möchte ich nur Objekte mit eindeutigen Ländernamen in meinem neuen Array haben.

Beispiel Arrays:

var result = [ 
    {country: 'united states', numofdistributors: 5}, 
    {country: 'united states', numofdistributors: 5}, 
    {country: 'brazil', numofdistributors: 2}, 
    {country: 'Germany', numofdistributors: 1}, 
    {country: 'india', numofdistributors: 6}, 
    {country: 'united states', numofdistributors: 5}, 
    {country: 'Egypt', numofdistributors: 1}, 
    {country: 'Germany', numofdistributors: 1}, 
]; 

Dies ist, was ich meine Array soll sich herausstellen, wie: ohne Glück und nur Frustration

var finalResult = [ 
    {country: 'united states', numofdistributors: 5}, 
    {country: 'brazil', numofdistributors: 2}, 
    {country: 'Germany', numofdistributors: 1}, 
    {country: 'india', numofdistributors: 6}, 
    {country: 'Egypt', numofdistributors: 1}, 
]; 

Hier ist mein Versuch, zu lösen:

var finalResults = []; 
result.forEach(function(country){ 
    if(finalResults.indexOf(country.country) != -1){ 
     console.log('already added'); 
    }else { 
     //console.log('Does not exist'); 
     finalResults.push(country); 
}); 
console.log(finalResults); 
+0

machte ich ein paar Änderungen am Code und meine Antwort aktualisiert – Mojtaba

Antwort

2

Sie könnten die Daten mit Array#filter filtern und ein Objekt für bereits eingefügtes Element verwenden s.

var result = [{ country: 'united states', numofdistributors: 5 }, { country: 'united states', numofdistributors: 5 }, { country: 'brazil', numofdistributors: 2 }, { country: 'Germany', numofdistributors: 1 }, { country: 'india', numofdistributors: 6 }, { country: 'united states', numofdistributors: 5 }, { country: 'Egypt', numofdistributors: 1 }, { country: 'Germany', numofdistributors: 1 }, ], 
 
    finalResult = result.filter(function (a) { 
 
     var key = a.country + '|' + a.numofdistributors; 
 
     if (!this[key]) { 
 
      this[key] = true; 
 
      return true; 
 
     } 
 
    }, Object.create(null)); 
 

 
document.write('<pre>' + JSON.stringify(finalResult, 0, 4) + '</pre>');

+1

Wow, nette Idee ein leeres Objekt als einen gemeinsam genutzten Speicher zur Verfügung zu stellen. –

+1

@Nina Scholz, vielen Dank! ersparte mir viel Kopfweh :) –

1

Sie es bereits getan. Nur ein einfacher Fehler:

var result = [ 
 
    {country: 'united states', numofdistributors: 5}, 
 
    {country: 'united states', numofdistributors: 5}, 
 
    {country: 'brazil', numofdistributors: 2}, 
 
    {country: 'Germany', numofdistributors: 1}, 
 
    {country: 'india', numofdistributors: 6}, 
 
    {country: 'united states', numofdistributors: 5}, 
 
    {country: 'Egypt', numofdistributors: 1}, 
 
    {country: 'Germany', numofdistributors: 1}, 
 
]; 
 

 
var finalResult = [ 
 
    {country: 'united states', numofdistributors: 5}, 
 
    {country: 'brazil', numofdistributors: 2}, 
 
    {country: 'Germany', numofdistributors: 1}, 
 
    {country: 'india', numofdistributors: 6}, 
 
    {country: 'Egypt', numofdistributors: 1}, 
 
]; 
 

 
var finalResults = []; 
 
var countries = []; 
 
result.forEach(function(country){ 
 
    if(countries.indexOf(country.country) != -1){ 
 
     console.log('already added'); 
 
    }else { 
 
     //console.log('Does not exist'); 
 
     finalResults.push(country); 
 
     countries.push(country.country); 
 
} 
 
} 
 
); 
 
console.log(finalResults);

+0

Wow, sollte wohl in Betracht gezogen werden, von meinem Computer ein wenig Abstand zu nehmen. Danke, dass du darauf hingewiesen hast. –

+0

@TravisMichaelHeller, ja. Manchmal brauchen wir eine Pause und frischen das Gehirn auf – Mojtaba

Verwandte Themen