May 6th, 2021
JS Functional Programming
img

What is .filter(Boolean)? Sometimes the functions quite confuse me and sometimes I endup writing big functional code for small Problems. Here I want to collect all my experience of Exceptional(but useful) functions that I learned.

I will just show examples here instead of going into details what they do as the examples are self explanatory.

filter(Boolean)

Copy 📋
[1,2,0,null,undefined,"",{}].filter(Boolean)
// [1,2,{}]

some(() => )

A good shortcut to find at least one val exists

Copy 📋
[1,1,2,3].some( val => val === 1 )
// true

every(() => )

Copy 📋
[1,1,2,3].every( val => val < 4 )
// true

find(() => ) / findIndex / lastIndexOf

Finds the first elem

Copy 📋
[1,1,4,2,3].find( val => val > 1 )
// 4

[1,1,4,2,3].findIndex( val => val > 1 )
// 2

[1,1,4,2,3].lastIndexOf( val => val > 1 )
// 4