2008 1 กันยายน 2008
ลบออกจากรายการที่ระบุอาร์เรย์จาวาสคริปต์ที่ตรงกับสายที่ผ่าน
นี่วิธีที่รวดเร็วของการขยายพื้นเมืองวัตถุ array JavaScript เพียงที่จะทำนั่นคือ ...
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;
}
ดังนั้นตอนนี้คุณสามารถทำอะไรเช่นนี้ ...
var animals= new Array("dog","lion","cat","tiger","elephant");
animals. removeItem ('tiger');ตอนนี้อาเรย์สัตว์จะมี "สุนัข", "สิงโต", "แมว", "ช้าง";
PS: และนี่คือต้นแบบ String Trim มากเกินไป ...
String.prototype.trim=function(str) {
str = this != window? this : str;
return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
เพลิดเพลินไปกับ ....










































