Ver Mensaje Individual
  #3 (permalink)  
Antiguo 03/07/2014, 10:22
Avatar de Alexis88
Alexis88
Philosopher
 
Fecha de Ingreso: noviembre-2011
Ubicación: Tacna, Perú
Mensajes: 5.552
Antigüedad: 12 años, 5 meses
Puntos: 977
Respuesta: Mostrar en un select los elementos de un array sin repetir

O también puedes construir tu propia función de filtrado que retorne un array con valores únicos:

Código Javascript:
Ver original
  1. var array_unique = function(array){
  2.     var array_filtrado = [];
  3.         if (!Array.prototype.forEach){
  4.             var total = array.length;
  5.             for (var i = 0; i < total; i++)
  6.                 if (!Array.prototype.indexOf){
  7.                     var repeticiones = false;
  8.                     for (var j = i + 1; j < total; j++)
  9.                         if (array[i] == array[j]){
  10.                             repeticiones = true;
  11.                             break;
  12.                         }
  13.                     if (repeticiones) array_filtrado.push(array[i]);
  14.                 }
  15.                 else
  16.                     if (array_filtrado.indexOf(array[i]) == -1)
  17.                         array_filtrado.push(array[i]);
  18.         }
  19.         else
  20.             array.forEach(function(valor){
  21.                 if (array_filtrado.indexOf(valor) == -1)
  22.                     array_filtrado.push(valor);
  23.             });
  24.         return array_filtrado;
  25.     };
  26.  
  27. console.log(array_unique([1, 3, 4, 2, 1, 2, 1, 3, 1])); //[1, 3, 4, 2]
  28. console.log(array_unique([5, 5, 1, 2, 1, 4, 5])); //[5, 1, 2, 4]

Saludos
__________________
«Juro por mi vida y mi amor por ella, que jamás viviré para el provecho de otro hombre, ni le pediré a otro hombre que viva para el mío».

Ayn Rand

Última edición por Alexis88; 04/07/2014 a las 20:14 Razón: Mejora para compatibilidad con IE5.5+