ตัวอย่างเป็น: ด้านล่าง:
คัดลอกรหัสรหัสดังนี้:
-
* ลบองค์ประกอบอาร์เรย์ผ่านค่า
-
* @param ค่าองค์ประกอบผสมค่า
* @return Array
-
array.prototype.deleteValue = function (value) {
var i = 0;
สำหรับ (ฉันอยู่ในนี้) {
ถ้า (นี่ [i] == ค่า) หยุด;
-
ส่งคืนสิ่งนี้ slice (0, i) .concat (this.slice (parseint (i, 10) + 1));
-
// ตัวอย่าง
การทดสอบ var = อาร์เรย์ใหม่ (1,5,3,4,2);
// เอาต์พุต 5
console.log (test.length);
// ลบองค์ประกอบด้วยค่า 4
ทดสอบ = test.deleteValue (4);
// output [1, 5, 3, 2]
console.log (ทดสอบ);
// เอาต์พุต 4
console.log (test.length);
-
* ลบองค์ประกอบอาร์เรย์ผ่านดัชนี
-
* @Param INT INDEX INDEX
* @return Array
-
array.prototype.deleteindex = ฟังก์ชั่น (ดัชนี) {
ส่งคืนสิ่งนี้ slice (0, index) .concat (this.slice (parseint (ดัชนี, 10) + 1));
-
// ตัวอย่าง
การทดสอบ var = อาร์เรย์ใหม่ (1,5,3,4,2);
// เอาต์พุต 5
console.log (test.length);
// ลบองค์ประกอบด้วยการจัดทำดัชนี 1
ทดสอบ = test.deleteIndex (1);
// output [1, 3, 4, 2]
console.log (ทดสอบ);
// เอาต์พุต 4
console.log (test.length);