I am trying to filter an array like this:
array.filter(e => { return e })
With this I want to filter all empty strings including undef
const justStrings = array.filter(element =>
(typeof element === 'string' || element instanceof String)
&& element
)
To be shure your element is a string
you have to check that it isn't a variable type (let str = 'hello'
) or an instance of String (new String('hello')
) because this case would return object
by typeof element
.
Additionally you have to check if your element exists.