Tuy nhiên bạn chọn làm điều đó, bắt đầu ngược lại và đếm ngược đơn giản nhất. Nó cũng phụ thuộc vào mảng của bạn là thưa thớt và nếu bạn muốn cho nó vẫn còn thưa thớt. Dễ nhất là tạo cho mình một chức năng tái sử dụng và thư viện của riêng bạn. Bạn có thể làm điều này. Nếu bạn đặt compress
thành true thì mảng của bạn sẽ trở thành mảng liên tục chứ không phải là mảng thưa thớt. Hàm này sẽ xóa tất cả các lần xuất hiện phù hợp của giá trị và sẽ trả về một mảng các phần tử đã xóa.
Javascript
function is(x, y) {
if (x === y) {
if (x === 0) {
return 1/x === 1/y;
}
return true;
}
var x1 = x,
y1 = y;
return x !== x1 && y !== y1;
}
function removeMatching(array, value /*, compress (default = false)*/) {
var removed = [],
compress = arguments[2],
index,
temp,
length;
if (typeof compress !== "boolean") {
compress = false;
}
if (compress) {
temp = [];
length = array.length;
index = 0;
while (index < length) {
if (array.hasOwnProperty(index)) {
temp.push(array[index]);
}
index += 1;
}
} else {
temp = array;
}
index = 0;
length = temp.length;
while (index < length) {
if (temp.hasOwnProperty(index) && is(temp[index], value)) {
if (compress) {
removed.push(temp.splice(index, 1)[0]);
} else {
removed.push(temp[index]);
delete temp[index];
}
}
index += 1;
}
if (compress) {
array.length = 0;
index = 0;
length = temp.length;
while (index < length) {
if (temp.hasOwnProperty(index)) {
array.push(temp[index]);
}
index += 1;
}
}
return removed;
}
var test = [];
test[1] = 1;
test[50] = 2;
test[100] = NaN;
test[101] = NaN;
test[102] = NaN;
test[200] = null;
test[300] = undefined;
test[400] = Infinity;
test[450] = NaN;
test[500] = -Infinity;
test[1000] = 3;
console.log(test);
console.log(removeMatching(test, NaN));
console.log(test);
console.log(removeMatching(test, Infinity, true));
console.log(test);
Output
[1: 1, 50: 2, 100: NaN, 101: NaN, 102: NaN, 200: null, 300: undefined, 400: Infinity, 450: NaN, 500: -Infinity, 1000: 3]
[NaN, NaN, NaN, NaN]
[1: 1, 50: 2, 200: null, 300: undefined, 400: Infinity, 500: -Infinity, 1000: 3]
[Infinity]
[1, 2, null, undefined, -Infinity, 3]
On jsfiddle
Bạn đã kiểm tra này [http://stackoverflow.com/questions/9882284/looping-through-array-and- câu trả lời xóa-mục-không-phá-cho-vòng lặp]? –
Bạn có thể giữ bước chuyển tiếp của bạn trong ví dụ đầu tiên và đặt bài viết của bạn giảm dần 'i' trực tiếp trong lệnh' .splice() ':' myarray.splice (i--, 1); ' –