javascript - filter method replacing one array with another array -


i'm trying filter out values of 1 array array. when using javascript's filter method, script looks 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 documentation, when console areas array after i've run filter function, array should this

"a area", "b area", "c area" 

however, that's not what's happening. instead, i'm getting values of notused array, it's replacing areas array notused array. can explain why happening , how go getting areas array without values of notused array?

if question has been asked, please let me know in comments , link answered question. way can delete 1 , eliminate duplication.

two mistakes.

  1. areas = notused assign value of notused areas, doesn't compare them.

  2. filter returns new array. doesn't change original array.

probably can write this

areas = areas.filter(function (area) {   return notused.indexof(area) === -1; }); 

now, notused.indexof(area) returns index of area in notused array. if couldn't find it, return -1.

also, see assign result of filter areas.


Comments

Popular posts from this blog

ios - Is 'init' forbidden as *part* of a variable name? -

javascript - Why Selenium can't find an element that is graphically visible -

angular - Angular2 Router: Cannot find primary outlet to load 'HomeComponent' -