I have two filter function but is possible to be in one line?
Check my code:
this is work fine but is possible to be on one line?
I have two filter function but is possible to be in one line?
Check my code:
this is work fine but is possible to be on one line?
2
Answers
Boolean
is just filtering for truthy values, so you can write the filter with just&&
:Well,
filter()
returns an array, andfilter()
can be called on an array. Which the code shown already demonstrates. So you can certainly remove the intermediate variable and just callfilter()
directly on the result of the previousfilter()
operation:You can also combine the logic into a single call to
filter()
. What isBoolean
in this case? Is it just looking for any values which are "truthy"? If so then you can do something like this:Or if it’s meant here to represent a function that you otherwise pass to
filter()
then you can invoke it just the same:Basically, yes… You can combine any boolean expressions into one larger expression or alternatively you can chain as many calls to
filter()
(or any other array method which returns an array) as you like.