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

3
  • 1
    please add the wanted result as well. Commented Sep 3, 2017 at 18:06
  • because the pop() method removes the last element from an array Commented Sep 3, 2017 at 18:10
  • Related or duplicate Commented Sep 3, 2017 at 18:12

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes the filter method is what I was looking for!
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.