Files
30-seconds-of-code/snippets/findLast.md
2018-01-24 13:02:07 +02:00

14 lines
341 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)[0];
```
```js
findLast([1, 2, 3, 4], n => n % 2 === 1); // 3
```