2017-05-17 4 views
0

Eingang zu entfernen:javascript: wie doppelte Arrays innerhalb Array von Arrays

[[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]] 

Der Ausgang Ich möchte:

[[-1,-1,2],[-1,0,1]] 

Irgendwelche anderen Ideen außer this one?

Dank

+1

Irgendein Grund, warum wir das nicht als ein Duplikat der Frage schließen sollten, mit der Sie selbst verbunden waren? Hat die Lösung in dieser Frage nicht für Sie funktioniert? – nnnnnn

+0

Einverstanden. Das sieht nach einer ziemlich guten Lösung aus. –

+0

@nnnnnn Weil das ein schrecklicher und langsamer Ansatz ist? – Bergi

Antwort

1

Sie erhalten um nicht wirklich die Arrays stringifying, da dies die einfachste (und recht schnell) Weg, um sie von Wert zu vergleichen ist. Also würde ich gehen für

Array.from(new Set(input.map(JSON.stringify)), JSON.parse) 

Siehe auch Remove Duplicates from JavaScript Array für andere Ansätze, obwohl die meisten von ihnen zwei Werte erfordern wird durch === vergleichbar.

0

jsfiddle

Leihen das Array Vergleichscode aus dieser post

// Warn if overriding existing method 
if(Array.prototype.equals) 
    console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code."); 
// attach the .equals method to Array's prototype to call it on any array 
Array.prototype.equals = function (array) { 
    // if the other array is a falsy value, return 
    if (!array) 
     return false; 

    // compare lengths - can save a lot of time 
    if (this.length != array.length) 
     return false; 

    for (var i = 0, l=this.length; i < l; i++) { 
     // Check if we have nested arrays 
     if (this[i] instanceof Array && array[i] instanceof Array) { 
      // recurse into the nested arrays 
      if (!this[i].equals(array[i])) 
       return false;  
     }   
     else if (this[i] != array[i]) { 
      // Warning - two different object instances will never be equal: {x:20} != {x:20} 
      return false; 
     }   
    }  
    return true; 
} 

var old = [[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]], n = []; 

while(old.length) { 
    var arr = old.shift(), matched = false; 

    for(var i = 0, len = n.length; i < len; i++) { 
    if (arr.equals(n[i])) { 
     matched = true; 
     break; 
    } 
    } 
    if (!matched) { 
    n.push(arr); 
    } 
} 
+0

Warum sollte man 'instanceof' über' Array.isArray' verwenden? Ich bin nur neugierig. –

+0

Es gibt keinen Unterschied. Siehe http://stackoverflow.com/questions/22289727/difference-between-using-array-isarray-and-instanceof-array –

0

können Sie eine HashMap erstellen und Werte darin speichern. Dies hält immer den letzten Wert.

var data = [[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]] 
 

 
var hashMap = {} 
 

 
data.forEach(function(arr){ 
 
    // If your subArrays can be in any order, you can use .sort to have consistant order 
 
    hashMap[arr.join("|")] = arr; 
 
}); 
 

 
var result = Object.keys(hashMap).map(function(k){ 
 
    return hashMap[k] 
 
}) 
 

 
console.log(result)

0

Es gibt bereits ein gutes Programm für das, versuchen Lodash, eine der Funktion davon _.uniqWith ist, mit dieser Funktion können Sie die folgenden Aktionen durchführen.

<script src="/path/to/lodash.js"></script> 
<script> 
    var aa = [[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]]; 
    console.log(aa); 
    console.log(_.uniqWith(aa,_.isEqual)); 
</script> 
Verwandte Themen