2008 O septembrie 2008

Scoateţi element specific dintr-o matrice JavaScript care se potriveşte cu un şir de trecut

Iată mod rapid de a extinde nativ obiect Javascript Array, doar pentru a face asta ...

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

Deci, acum puteţi face ceva de genul asta ...

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

Acum, matrice animale va conţine "câine", "leu", "pisica", "elefant";

PS: Si aici este prototipul String Trim prea ...

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


Bucuraţi-vă de ....


NDK acasă | Exprimarea IT | Gust Exprimarea | Penmenship Exprimarea | Awe Exprimarea | a mă exprima