ramda
Version:
A practical functional library for JavaScript programmers.
35 lines • 1.17 kB
JavaScript
var _curry2 = /*#__PURE__*/require("./internal/_curry2.js");
var _dispatchable = /*#__PURE__*/require("./internal/_dispatchable.js");
var _xfindLast = /*#__PURE__*/require("./internal/_xfindLast.js");
/**
* Returns the last element of the list which matches the predicate, or
* `undefined` if no element matches.
*
* Acts as a transducer if a transformer is given in list position.
*
* @func
* @memberOf R
* @since v0.1.1
* @category List
* @sig (a -> Boolean) -> [a] -> a | undefined
* @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`.
* @see R.transduce
* @example
*
* const xs = [{a: 1, b: 0}, {a:1, b: 1}];
* R.findLast(R.propEq(1, 'a'))(xs); //=> {a: 1, b: 1}
* R.findLast(R.propEq(4, 'a'))(xs); //=> undefined
*/
var findLast = /*#__PURE__*/_curry2( /*#__PURE__*/_dispatchable([], _xfindLast, function findLast(fn, list) {
var idx = list.length - 1;
while (idx >= 0) {
if (fn(list[idx])) {
return list[idx];
}
idx -= 1;
}
}));
module.exports = findLast;