1

I am looping through elements in an array. If a value is not within a specific range I want to remove the element (that the value is associated with).

Here's what i have so far (see code below) - I am using the pop method to remove the element as it comes through but the last element in the array always gets removed and NOT the element/value within the if/then statement. I've tried the splice method as well but I can't get that to work. Any idea on how to do this?

    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 (){

    for (var r = 0; r <= h.length; r++){
        var p = h[r][0];
     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();
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
David Bailey
  • 133
  • 3
  • 9

2 Answers2

3

It's not good practice to remove elements from an array while iterating through it, instead, you can filter it using filter(), like this :

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);

Or, in case you want to display messages, you can use another array and only push the elements you want to it (better use a 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]){
     console.log(address);
      g.push(r); // push it
    }
    else{
      console.log(address + " - OVER 33 - REMOVED"); // do nothing
    }
  });
  h = g;
}

Location();
console.log(h);
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
1

For removing elements of an array, you could loop from the end and splice if necessary, because the next smaller index is not affected.

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392