Home Reference Source

ts_temp/array/findLast.js

import curryN from '../function/curryN';
/**
 * Returns the last element of the list which matches the predicate, or
 * `undefined` if no element matches.
 *
 * @param {Function} fn The predicate function used to determine if the element is the
 * desired one.
 * @param {Array} list The array to consider.
 * @return {Object} The element found, or `undefined`.
 * @example
 *
 *      const xs = [{a: 1, b: 0}, {a:1, b: 1}];
 *
 *      findLast(propEq('a', 1))(xs); //=> {a: 1, b: 1}
 *      findLast(propEq('a', 4))(xs); //=> undefined
 */
export default curryN(2, (fn, list) => {
    for (let i = list.length - 1; i >= 0; i--) {
        if (fn(list[i], i, list)) {
            return list[i];
        }
    }
});