2017-09-03 52 views
1

我正在循環數組中的元素。如果某個值不在特定範圍內,我想刪除該元素(與該值關聯)。如果值通過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();

+1

請添加想要的結果也是如此。 –

+0

,因爲_the pop()方法從數組中刪除最後一個元素_ – yvoytovych

+0

[相關或重複](https://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for -loop) –

回答

3

這不是很好的做法,通過它迭代從數組中刪除元素,相反,則可以使用filter()過濾,像這樣:

var h = [ 
 
    ["29","Verbena St", "500", "2", "2,702"], 
 
    ["36", "Quitman St", "400", "2", "1,700"], 
 
    ["32", "Alan Dr", "500", "2", "2,408"], 
 
    ["34", "Newton St", "300", "2", "1,954"], 
 
    ["30", "Soth Pl", "400", "2", "1,509"] 
 
]; 
 

 
var hs = [ 
 
    ["Verbena St"], 
 
    ["Quitman St"], 
 
    ["Alan Dr"], 
 
    ["Newton St"], 
 
    ["Soth Pl"] 
 
]; 
 

 
function Location(){ 
 
    h = h.filter(function(r, i){ // r = value, i = index 
 
    var p=r[0]; 
 
    var address = r[1]; // Get address 
 
    return p >= 21 && p <= 33 && address == hs[i]; 
 
    }); 
 
}; 
 

 
Location(); 
 
console.log(h);

或者,如果你想顯示消息ES,您可以使用另一個陣列,只推你想它的元素(最好使用foreach):

var h = [ 
 
    ["29","Verbena St", "500", "2", "2,702"], 
 
    ["36", "Quitman St", "400", "2", "1,700"], 
 
    ["32", "Alan Dr", "500", "2", "2,408"], 
 
    ["34", "Newton St", "300", "2", "1,954"], 
 
    ["30", "Soth Pl", "400", "2", "1,509"] 
 
]; 
 

 
var hs = [ 
 
    ["Verbena St"], 
 
    ["Quitman St"], 
 
    ["Alan Dr"], 
 
    ["Newton St"], 
 
    ["Soth Pl"] 
 
]; 
 

 
function Location(){ 
 
    var g = []; 
 
    h.forEach(function(r, i){ // r = value, i = index 
 
    var p = r[0]; 
 
    var address = r[1]; // Get address 
 
    if (p >= 21 && p <= 33 && address == hs[i]){ 
 
    \t console.log(address); 
 
     g.push(r); // push it 
 
    } 
 
    else{ 
 
     console.log(address + " - OVER 33 - REMOVED"); // do nothing 
 
    } 
 
    }); 
 
    h = g; 
 
} 
 

 
Location(); 
 
console.log(h);

+0

是的,過濾方法就是我一直在尋找的! –

1

用於去除數組的元素,你可以從端部線圈和必要時進行拼接,因爲下一個較小的索引不受影響。

function updateLocation() { 
 
    var address, p, r = h.length; 
 
    while (r--) { 
 
     p = h[r][0]; 
 
     address = h[r][1]; // Get address 
 
     if (p >= 21 && p <= 33 && address == hs[r]) { 
 
      console.log(address); 
 
     } else { 
 
      console.log(address + " - OVER 33"); 
 
      h.splice(r, 1); 
 
      console.log(address + " - REMOVED"); 
 
     } 
 
    } 
 
} 
 

 
var h = [["29", "Verbena St", "500", "2", "2,702"], ["36", "Quitman St", "400", "2", "1,700"], ["32", "Alan Dr", "500", "2", "2,408"], ["34", "Newton St", "300", "2", "1,954"], ["30", "Soth Pl", "400", "2", "1,509"]], 
 
    hs = [["Verbena St"], ["Quitman St"], ["Alan Dr"], ["Newton St"], ["Soth Pl"]]; 
 

 
updateLocation(); 
 
console.log(h);

相關問題