DEV Community

Discussion on: Which Array Function When?

Collapse
 
darrenkopp profile image
Darren Kopp

Nice article! Everyone seems to forget my two favorite functions though, some and every

If you've ever written something like

const items= [1, 9, 4, 2, 42];

// checks if any item in the array matches the predicate
var hasAnyBigItems = items.some(item => item > 10));

// checks if every item in the array matches the predicate
var allPositive = items.every(item => item > 0);

If you ever find yourself checking the length property after items.filter(), you can likely use some or every to do the same thing without the array generation penalty.

Collapse
 
leydsonvieira profile image
Leydson Vieira

Really nice!!! I'm a Python programmer and didn't know JS has functions like that!! Congrats bro