Rimuovi elemento specifico da un array javascript che corrisponde a una stringa passata
Ecco modo veloce di estendere la nativa Object Array Javascript, giusto per farlo ...
Array.prototype. removeItem =function(str) {
for(i=0; i<this.length ; i++){
if(escape(this[i]).match(escape(str.trim()))){
this.splice(i, 1); break;
}
}
return this;
}
Così ora si può fare qualcosa di simile ...
var animals= new Array("dog","lion","cat","tiger","elephant");
animals. removeItem ('tiger');Ora l'array conterrà gli animali "cane", "leone", "cat", "elefante";
PS: E qui è il prototipo String Trim troppo ...
String.prototype.trim=function(str) {
str = this != window? this : str;
return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
Godetevi ....










































