January 2020
Intermediate to advanced
470 pages
11h 13m
English
As with the other methods, let's finish this section by studying how to implement the methods we showed by using the omnipotent reduce(). This is a good exercise to get accustomed to working with higher-order functions, even if you are never going to actually use these polyfills!
The find() function requires a bit of work. We start the search with an undefined value, and if we find an array element so that the predicate is true, we change the accumulated value to that of the array:
arr.find(fn);// arr.reduce((x, y) => (x === undefined && fn(y) ? y : x), undefined);
For findIndex(), we must remember that the callback function receives the accumulated value, the array's current element, and the ...
Read now
Unlock full access