我正在循環數組中的元素。如果某個值不在特定範圍內,我想刪除該元素(與該值關聯)。如果值通過if/then語句從數組中移除元素 - Javascript
這是我到目前爲止(請參閱下面的代碼) - 我使用彈出方法刪除元素,因爲它通過,但數組中的最後一個元素總是被刪除,而不是元素/值在if /然後聲明。我也嘗試了拼接方法,但是我無法讓它工作。任何想法如何做到這一點?
var h = [
\t ["29","Verbena St", "500", "2", "2,702"],
\t ["36", "Quitman St", "400", "2", "1,700"],
\t ["32", "Alan Dr", "500", "2", "2,408"],
\t ["34", "Newton St", "300", "2", "1,954"],
\t ["30", "Soth Pl", "400", "2", "1,509"]
];
var hs = [
\t ["Verbena St"],
\t ["Quitman St"],
["Alan Dr"],
\t ["Newton St"],
\t ["Soth Pl"]
];
function Location(){
for (var r = 0; r <= h.length; r++){
var p = h[r][0];
\t var address = h[r][1]; // Get address
if (p >= 21 && p <= 33 && address == hs[r]){
console.log(address);
}
else {
console.log(address + " - OVER 33");
h.pop(address);
console.log(address + " - REMOVED");
}
}
};
Location();
請添加想要的結果也是如此。 –
,因爲_the pop()方法從數組中刪除最後一個元素_ – yvoytovych
[相關或重複](https://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for -loop) –