2016-08-02 5 views
0

Winkel js Entfernen von doppelten Schlüsselfeld aus allen Arrays innerhalb eines Arrays

app.filter('unique', function() { 
 
    return function(collection, keyname) { 
 
     var output = [], 
 
      keys = []; 
 

 
     angular.forEach(collection, function(item) { 
 
      var key = item[keyname]; 
 
      if(keys.indexOf(key) === -1) { 
 
       keys.push(key); 
 
       output.push(item); 
 
      } 
 
     }); 
 

 
     return output; 
 
    }; 
 
}); 
 

 

 
/* mainOptions = [ a1[],b2[],c3[]] 
 
    subOptions a1[] = [fuzzyoptions{ optionid : 11},fuzzyoptions{ optionid : 13},fuzzyoptions{ optionid : 11},fuzzyoptions{ optionid : 12}] 
 
subOptions b1[] = [fuzzyoptions{ optionid : 12},fuzzyoptions{ optionid : 13},fuzzyoptions{ optionid : 11},fuzzyoptions{ optionid : 12}] 
 
    subOptions b1[] = [fuzzyoptions{ optionid : 19},fuzzyoptions{ optionid : 23},fuzzyoptions{ optionid : 11},fuzzyoptions{ optionid : 12}] */ 
 

 
/* I want ng-repeat to remove duplicates for optionid in all arrays inside one main array */ 
 

 
/*so check will go for fuzzyoptions{ optionid : 11},fuzzyoptions{ optionid : 13},fuzzyoptions{ optionid : 12},fuzzyoptions{ optionid : 19},fuzzyoptions{ optionid : 23} only 5 times*/
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script> 
 
<div ng-repeat = "main in mainOptions"> 
 
    <div ng-repeat = "sub in main.SubOptions | unique: 'sub.fuzzyoptions.optionid'"> 
 
    </div> 
 
    </div>

I doppelte optionids Feld entfernt werden soll; s Werte von allen Suboption Arrays innerhalb mainOptions Arrays. Ich möchte erreichen, doppelte Schlüssel-Wert-Felder für alle Arrays in einem Eltern-Array mit benutzerdefinierten Filter oder verfolgen durch oder jede andere Lösung mit Winkel js

Antwort

0

Ich habe eine small fiddle, die verbessert werden kann, um Ihre Bedürfnisse zu erfüllen. Es ist ein Filter, der einen Schlüssel und eine Liste empfängt und die Liste mit den eindeutigen Werten dieses Schlüssels zurückgibt.

.filter('unique', function() { 
    return function(collection, keyname) { 
     var output = {}; 

     for(var i = 0; i < collection.length; i++){ 
      output[collection[i][keyname]] = collection[i]; 
     } 

     output = Object.keys(output).map(function (key) {return output[key]}); 

     return output; 
    }; 
}); 
0

versuchen Sie dies in Ihrem html wie (ng-repeat = "Rekord in Aufzeichnungen | removeDuplicate"), es wird funktionieren

.filter('removeDuplicate', function() { 
    return function(array) { 
    var array1 = []; 
    for(var i = 0; i < array.length; i++){ 
     for(var key1 in array[i]){ 
     var notfind=true; 
     if(array1 && (array1.length==0)){ 
     array1.push(array[i]); 
      } 
     for(var j=0;j<array1.length;j++){ 
      for(var key in array1[j]){ 
      if(array1[j][key]==array[i][key1]){ 
       notfind=false; 
       }}} 
      if(notfind){ 
       array1.push(array[i]) 
        }}} 
        return array1; 
       }; 
         }); 
Verwandte Themen