2008 01.09.2008

Odstrániť určité položky z poľa javascript, ktorý zodpovedá prešiel reťazec

Tu je rýchly spôsob, ako rozšíriť pôvodnú Javascript Array objekt, stačí k tomu, že ...

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

Takže teraz môžete urobiť niečo také ...

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

Teraz zvierat pole bude obsahovať "pes", "lev", "mačka", "slon";

PS: A tu je String trim prototyp príliš ...

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


Vychutnajte si ....


NDK domov | Vyjadrenie IT | Vyjadrenie poschodia | Vyjadrenie Penmenship | Vyjadrenie Awe | Vyjadrenie Myself