Contoh sebagai: di bawah:
Salin kode kode sebagai berikut:
/**
* Hapus elemen array melalui nilai
*
* @param nilai campuran nilai unsur
* @Return array
*/
Array.prototype.deletEvalue = function (value) {
var i = 0;
untuk (saya di ini) {
if (ini [i] == value) break;
}
Kembalikan ini.
}
// Contoh
var test = array baru (1,5,3,4,2);
// output 5
console.log (test.length);
// Hapus elemen dengan nilai 4
test = test.deletevalue (4);
// output [1, 5, 3, 2]
console.log (tes);
// output 4
console.log (test.length);
/**
* Hapus elemen array melalui indeks
*
* Indeks indeks int @param int
* @Return array
*/
Array.prototype.deleteIndex = function (index) {
Kembalikan this.slice (0, index) .concat (this.slice (parseInt (index, 10) + 1));
}
// Contoh
var test = array baru (1,5,3,4,2);
// output 5
console.log (test.length);
// Hapus elemen dengan indeks 1
test = test.deleteIndex (1);
// output [1, 3, 4, 2]
console.log (tes);
// output 4
console.log (test.length);