Marc Reichelt: Doppelte/Gleiche Werte aus Array heraus filtern

Beitrag lesen

Hallo Vanessa,

Nun soll nur _ein_ mal 'abc' und auch nur _ein_ mal 'def' ausgegeben werden. Wie kann ich das anstellen? Das ist zu hoch für mich.. =((

Für eigene Zwecke habe ich mir eine Funktion unique geschrieben, die das gleiche macht wie Murphy es beschreibt - aber direkt als Funktion jedes Arrays angesprochen werden kann.

/**  
 * Looks for duplicate entries and creates an array that contains each value only once.  
 * Leaves the original array untouched.  
 *  
 * @return Array without duplicate entries.  
 */  
if (!Array.prototype.unique) {  
 Array.prototype.unique = function() {  
  var uniqueArray = [];  
  
  for (var i = 0; i < this.length; i++) {  
   var found = false;  
   for (var j = 0; j < uniqueArray.length; j++) {  
    if (uniqueArray[j] == this[i]) {  
     found = true;  
     break;  
    }  
   }  
  
   if (!found) {  
    uniqueArray.push(this[i]);  
   }  
  }  
  
  return uniqueArray;  
 };  
}

Wenn du diesen Code eingebunden hast kannst du mit
Tag = Tag.unique();
dein Array um doppelte Einträge bereinigen.

Grüße

Marc Reichelt || http://www.marcreichelt.de/

--
panic("Oh boy, that early out of memory?");
        linux-2.2.16/arch/mips/mm/init.c
Selfcode: ie:{ fl:| br:> va:} ls:< fo:} rl:( n4:( ss:) de:> js:| ch:? sh:| mo:) zu:)