Trying to figure out what the ? and : mean in this one line return statement.
return input && input.length ? [input.filter(p => p > 0).length, input.filter(n => n < 0).reduce((a, b) => a + b, 0)] : [];
Trying to figure out what the ? and : mean in this one line return statement.
return input && input.length ? [input.filter(p => p > 0).length, input.filter(n => n < 0).reduce((a, b) => a + b, 0)] : [];
This is a ternary if statement, I'd use parenthesis for organization sake, but it works like this:
[condition] ? [returns if condition is true] : [returns if condition is false]
It means that if the condition before ?
is true then use the first part before :
else use the second part.
In your case it means that if input is not null and has value, then return the filtered one otherwise return an empty array.