Skip to Content
Mastering JavaScript Functional Programming - Second Edition
book

Mastering JavaScript Functional Programming - Second Edition

by Federico Kereki
January 2020
Intermediate to advanced
470 pages
11h 13m
English
Packt Publishing
Content preview from Mastering JavaScript Functional Programming - Second Edition

Higher-level predicates – some, every

The last functions we are going to consider greatly simplify going through arrays to test for conditions. These functions are as follows:

  • every(), which is true if and only if every element in the array satisfies a given predicate
  • some(), which is true if at least one element in the array satisfies the predicate

For example, we could easily check our hypothesis about all the countries having negative coordinates:

markers.every(v => v.lat < 0 && v.lon < 0); // falsemarkers.some(v => v.lat < 0 && v.lon < 0);  // true

If we want to find equivalents to these two functions in terms of reduce(), the two alternatives show nice symmetry:

arr.every(fn);// arr.reduce((x, y) => x && fn(y), true);arr.some(fn);
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering JavaScript Functional Programming

Mastering JavaScript Functional Programming

Federico Kereki

Publisher Resources

ISBN: 9781839213069Supplemental Content