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

338 B

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.

const findLast = (arr, fn) => arr.filter(fn).slice(-1);
findLast([1, 2, 3, 4], n => n % 2 === 1); // 3