Reputation: 433
I'm trying to filter a character array so that it doesn't include blank spaces or periods. Why would the following code not work?
arr.filter(char => char !== ' ' || char !== '.')
Upvotes: 0
Views: 49
Reputation: 1265
When we use multi condition to express our idea, it produces bug more easily. In your case, using a filer_array is good idea, plain and more maintainable:
let filter_array = [' ', '.']
let arr = Array.from(' .test. ')
arr.filter(char => !filter_array.includes(char))
Upvotes: 1
Reputation: 1072
You need to use and (&&)
, not or (||)
.
arr.filter(char => char !== ' ' && char !== '.')
Upvotes: 3