1

I'm trying to filter out the values of one array from another array. When using Javascript's filter method, my script looks like this.

var notUsed = ["Advanced Tac. Training Area", "Dunbarton Railroad Yard"];

var areas = ["A Area", "Advanced Tac. Training Area", "B Area", "Dunbarton Railroad Yard", "C Area"];

areas.filter(function(){
    return areas = notUsed;
})
console.log(areas);

According to the documentation, when I console the areas array after I've run the filter function, the array should look like this

"A Area", "B Area", "C Area"

However, that's not what's happening. Instead, I'm getting the values of the notUsed array, so it's essentially replacing the areas array with the notUsed array. Can anyone explain why this is happening and how I go about getting the areas array without the values of the notUsed array?

If this question has already been asked, please let me know in the comments and link to the answered question. that way I can delete this one and eliminate the duplication.

4 Answers 4

1

Two mistakes.

  1. areas = notUsed will simply assign the value of notUsed to areas, it doesn't compare them.

  2. filter returns a new array. It doesn't change the original array.

Probably you can write it like this

areas = areas.filter(function (area) {
  return notUsed.indexOf(area) === -1;
});

Now, the notUsed.indexOf(area) returns the index of the area in the notUsed array. If it couldn't find it, then it will return -1.

Also, see we assign the result of filter back to areas.

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

2 Comments

Awesome, thank you. Do you know why this isn't something that's explained or demonstrated in the documentation? I know that it explains that the original array is not changed, but it doesn't demonstrate how to do anything like this. At least, not that I've seen.
@Robert There are few examples in MDN.
0

You have not understood the use of filter correctly.

var notUsed = ["Advanced Tac. Training Area", "Dunbarton Railroad Yard"];

var areas = ["A Area", "Advanced Tac. Training Area", "B Area", "Dunbarton Railroad Yard", "C Area"];

var res = areas.filter(function(ele){
    return (notUsed.indexOf(ele) == -1)
})
console.log(res);

Comments

0

The Filter function should test the values in the array against a condition implemented by you. This is not the case in your example. Filter Function

This could be a possible solution for you.

var notUsed = ["Advanced Tac. Training Area", "Dunbarton Railroad Yard"];

var areas = ["A Area", "Advanced Tac. Training Area", "B Area", "Dunbarton Railroad Yard", "C Area"];

var filtered = areas.filter(value => { return notUsed.indexOf(value) === -1 })
console.log(filtered);

Comments

0

filter() method creates a new array with all elements that pass the test implemented by the provided function.

  var notUsed = ["Advanced Tac. Training Area", "Dunbarton Railroad Yard"];    
    var areas = ["A Area", "Advanced Tac. Training Area", "B Area", "Dunbarton Railroad Yard", "C Area"];    
    var out = areas.filter(function(index) {
     return notUsed.indexOf(index) == -1;
    });
    console.log(out);

Comments

Your Answer

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