Comunidad de diseño web y desarrollo en internet online

listar elementos no repetidos de un array unidimensional

Citar            
MensajeEscrito el 28 Nov 2006 05:52 pm
Hola a tod@s!

tengo un problemón con un array unidimensional con muchos elementos aunque pocos distintos, necesito listar sólo los diferentes, he probado con hacer otro array para introducir los elementos (vía array.push) y checkeando uno por uno si existe ya en el array2, pero me entra en un bucle infinito y peta todo... :crap:

alguien tiene alguna sugerencia???

muchisimas gracias,
saludos!

Por rcosta

1 de clabLevel



Genero:Masculino  

firefox
Citar            
MensajeEscrito el 28 Nov 2006 07:14 pm

Código :

//Powered by Dano
function arrayDistinct(targetArray:Array):Array
{
  var alDistinct:Array;
  alDistinct = new Array();
  for(var i in targetArray)
  {
    if( !inArray(targetArray[i], alDistinct) )
    {
      alDistinct.push(targetArray[i]);
    }
  }
  return alDistinct;
}
function inArray(value:Object, targetArray:Array):Boolean{
  for(var i in targetArray)
  {
    if( targetArray[i] == value )
      return true;
  }  
  return false;
}
//Powered by Dano

/*********************************************************/
//Ejemplo de uso
var Test:Array;
Test = new Array(1,6,9,1,1,6,9,6,6);
trace(arrayDistinct(Test));
Test = new Array("hola","sip","iep","hola","sip","sip","sip","iep");
trace(arrayDistinct(Test));

Por Dano

BOFH

4273 de clabLevel

14 tutoriales
4 articulos
10 ejemplos

Genero:Masculino   Bastard Operators From Hell Premio_Secretos

Lugar estratégico para vigilarte

firefox
Citar            
MensajeEscrito el 29 Nov 2006 08:50 am
Para cualquier tipo de array, unidimensional o multidimensional esta es rapidísima. Muy util si son muchos datos.

Código :

Array.prototype.eliminaDuplicados = function() {
    var obj = {}, i = this.length, j = 0, arr = [], t, tp, o_arr = [], o;
    while (i--) { 
    tp=typeof (t=this[i]);
    if(tp=="number" || tp=="string") {obj[t]=t}
    else if (!t.$$$){o_arr.push(t),t.$$$=true}}
    for (i in obj) {arr.push(obj[i])} 
    if (i=o_arr.length) {while (i--) {
      delete (o=o_arr[i]).$$$, arr.push(o)}}
    return arr;
 };


y llamada :

Código :

un_array.eliminaDuplicados();

Por Teseo

SWAT Team

1780 de clabLevel

14 tutoriales

Genero:Masculino   SWAT

msie
Citar            
MensajeEscrito el 29 Nov 2006 09:14 am
Hola!

perfecto, muchísimas gracias, estaba encalladísimo!

gracias otra vez a los dos!!!
^^

Por rcosta

1 de clabLevel



Genero:Masculino  

firefox

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.