2008 1 setembre 2008

Treure element específic d'una matriu de JavaScript que coincideix amb una cadena que es passa

Aquí hi ha manera ràpida d'ampliar l'objecte natiu de JavaScript Array, només per fer que ...

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

Així que ara vostè pot fer alguna cosa com això ...

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

Ara la matriu contindrà els animals "gos", "lleó", "gat", "elefant";

PD: I aquí hi ha el prototip de cadena retallada massa ...

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


Gaudeixi de ....


NDK a casa | Expressant IT | Boca Expressant | Penmenship Expressant | Awe Expressant | expressar-me