Home » Javascript » functional programming javascript chain filter operation defaults

functional programming javascript chain filter operation defaults

Posted by: admin August 19, 2018 Leave a comment

Questions:

Is there a way to filter an array and if it returns no objects default to the default option in a single chain?

e.g.

[1,2,3]
    .filter(isDivisibleByTen)
    // otherwise return whatever

I could write it like so

const result [1,2,3].filter(isDivisibleByTen) 
result ? result [0]
Answers: