Files
30-seconds-of-code/snippets/findLast.md
30secondsofcode 1adec8ce3f Travis build: 1187
2018-01-11 11:53:19 +00:00

14 lines
338 B
Markdown

### 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
```