JavaScript Array Remove
December 4th, 2007Recientemente, John Resig, escribió un método para borrar elementos de un array, que por cierto esta bastante fumado:
1 // Array Remove - By John Resig (MIT Licensed)
2 Array.prototype.remove = function(from, to) {
3 var rest = this.slice((to || from) + 1 || this.length);
4 this.length = from < 0 ? this.length + from : from;
5 return this.push.apply(this, rest);
6 };
7
8 //Ejemplos
9 // Remove the second item from the array
10 array.remove(1);
11 // Remove the second-to-last item from the array
12 array.remove(-2);
13 // Remove the second and third items from the array
14 array.remove(1,2);
15 // Remove the last and second-to-last items from the array
16 array.remove(-2,-1);
Hace un tiempo, tuve la necesidad de borrar todos los items de un array pero sin destruir el array, por unas razones especificas, y lo que hice fue lo siguiente:
var len = serialesArray.push(1 );
for (var i = 0; i < len ; i++ ){
var e = serialesArray.pop( );
}
Como ven, es un código bastante mortal :D, pues tengo poco tiempo usando javascript, la curva de aprendizaje del lenguaje se ve afectada por la cantidad de tiempo que le dedico a leer, que por los momentos es poco :D.










































