diff --git a/snippets/findLast.md b/snippets/findLast.md new file mode 100644 index 000000000..9dfa824d3 --- /dev/null +++ b/snippets/findLast.md @@ -0,0 +1,13 @@ +### findLast + +Returns the last element for which the provided function returns a truthy value. + +Use `Array.filter()` to remove elements for which `fn` returns falsey values, `Array.slice(-1)` to get the last one. + +```js +const findLast = (arr, fn) => arr.filter(fn).slice(-1); +``` + +```js +findLast([1, 2, 3, 4], n => n % 2 === 1) // 3 +``` diff --git a/tag_database b/tag_database index 45bce9b45..73d0a8310 100644 --- a/tag_database +++ b/tag_database @@ -44,6 +44,7 @@ extendHex:utility,string factorial:math,recursion fibonacci:math,array filterNonUnique:array +findLast:array flatten:array flip:adapter,function forEachRight:array,function