JorahFriendzone
JorahFriendzone

Reputation: 433

How to filter based off multiple values?

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

Answers (2)

LiuXiMin
LiuXiMin

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

Seth
Seth

Reputation: 1072

You need to use and (&&), not or (||).

arr.filter(char => char !== ' ' && char !== '.')

Upvotes: 3

Related Questions