2008 1 septembre 2008

Supprimer l'élément spécifique à partir d'un tableau javascript qui correspond à une chaîne passée

Voici moyen rapide d'étendre l'objet Array natif Javascript, juste pour le faire ...

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;
}

Donc maintenant vous pouvez faire quelque chose comme ça ...

var animals= new Array("dog","lion","cat","tiger","elephant");
animals. removeItem ('tiger');

Maintenant le tableau des animaux contiendra «chien», «lion», «chat», «éléphant»;

PS: Et voici le prototype string trim trop ...

String.prototype.trim=function(str) {
str = this != window? this : str;
return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}


Profitez ....


NDK la maison | Exprimant IT | Bouche Exprimant | Penmenship Exprimant | Awe Exprimant | m'exprimer