Add findLast
This commit is contained in:
13
snippets/findLast.md
Normal file
13
snippets/findLast.md
Normal file
@ -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
|
||||||
|
```
|
||||||
@ -44,6 +44,7 @@ extendHex:utility,string
|
|||||||
factorial:math,recursion
|
factorial:math,recursion
|
||||||
fibonacci:math,array
|
fibonacci:math,array
|
||||||
filterNonUnique:array
|
filterNonUnique:array
|
||||||
|
findLast:array
|
||||||
flatten:array
|
flatten:array
|
||||||
flip:adapter,function
|
flip:adapter,function
|
||||||
forEachRight:array,function
|
forEachRight:array,function
|
||||||
|
|||||||
Reference in New Issue
Block a user