Files
30-seconds-of-code/snippets/findLastN.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

30 lines
813 B
Markdown

---
title: findLastN
tags: array,intermediate
firstSeen: 2021-05-09T13:31:36+03:00
lastUpdated: 2021-05-09T13:31:36+03:00
---
Finds the last `n` elements for which the provided function returns a truthy value.
- Use a `for` loop to execute the provided `matcher` for each element of `arr`.
- Use `Array.prototype.unshift()` to prepend elements to the results array and return them if its `length` is equal to `n`.
```js
const findLastN = (arr, matcher, n = 1) => {
let res = [];
for (let i = arr.length - 1; i >= 0; i--) {
const el = arr[i];
const match = matcher(el, i, arr);
if (match) res.unshift(el);
if (res.length === n) return res;
}
return res;
};
```
```js
findLastN([1, 2, 4, 6], n => n % 2 === 0, 2); // [4, 6]
findLastN([1, 2, 4, 6], n => n % 2 === 0, 5); // [2, 4, 6]
```