diff --git a/README.md b/README.md index 078ad19d4..d67863f68 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ average(1, 2, 3); * [`everyNth`](#everynth) * [`filterNonUnique`](#filternonunique) * [`findLast`](#findlast) +* [`findLastIndex`](#findlastindex) * [`flatten`](#flatten) * [`forEachRight`](#foreachright) * [`groupBy`](#groupby) @@ -884,7 +885,7 @@ 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); +const findLast = (arr, fn) => arr.filter(fn).slice(-1)[0]; ```
@@ -899,6 +900,33 @@ findLast([1, 2, 3, 4], n => n % 2 === 1); // 3
[⬆ Back to top](#table-of-contents) +### findLastIndex + +Returns the index of the last element for which the provided function returns a truthy value. + +Use `Array.map()` to map each element to an array with its index and value. +Use `Array.filter()` to remove elements for which `fn` returns falsey values, `Array.slice(-1)` to get the last one. + +```js +const findLastIndex = (arr, fn) => + arr + .map((val, i) => [i, val]) + .filter(val => fn(val[1], val[0], arr)) + .slice(-1)[0][0]; +``` + +
+Examples + +```js +findLastIndex([1, 2, 3, 4], n => n % 2 === 1); // 2 (index of the value 3) +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### flatten Flattens an array up to the specified depth. diff --git a/docs/index.html b/docs/index.html index e44691aac..213aae959 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -133,8 +133,14 @@ Object.assig
 
everyNth([1, 2, 3, 4, 5, 6], 2); // [ 2, 4, 6 ]
 

filterNonUnique

Filters out the non-unique values in an array.

Use Array.filter() for an array containing only the unique values.

const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
 
filterNonUnique([1, 2, 2, 3, 4, 4, 5]); // [1,3,5]
-

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

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

findLastIndex

Returns the index of the last element for which the provided function returns a truthy value.

Use Array.map() to map each element to an array with its index and value. Use Array.filter() to remove elements for which fn returns falsey values, Array.slice(-1) to get the last one.

const findLastIndex = (arr, fn) =>
+  arr
+    .map((val, i) => [i, val])
+    .filter(val => fn(val[1], val[0], arr))
+    .slice(-1)[0][0];
+
findLastIndex([1, 2, 3, 4], n => n % 2 === 1); // 2 (index of the value 3)
 

flatten

Flattens an array up to the specified depth.

Use recursion, decrementing depth by 1 for each level of depth. Use Array.reduce() and Array.concat() to merge elements or arrays. Base case, for depth equal to 1 stops recursion. Omit the second argument, depth to flatten only to a depth of 1 (single flatten).

const flatten = (arr, depth = 1) =>
   depth != 1
     ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flatten(v, depth - 1) : v), [])