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
You could check for a string and empty both in your filter method:
array.filter(e => (typeof e === 'string') && !!e)
Note: !!e
returns false
if the element is null
, undefined
, ''
or 0.
I should mention that the "arrow"-function syntax only works in browsers that support ES6 or higher.
The alternative is:
array.filter(function(e) {
return (typeof e === 'string') && !!e;
});
Note: Keep in mind that Array.prototype.filter
doesn't exist in older browsers.