diff --git a/README.md b/README.md index b5e5c1f98..ba45cf676 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ average(1, 2, 3); * [`filterNonUnique`](#filternonunique) * [`flatten`](#flatten) * [`flattenDepth`](#flattendepth) +* [`forEachRight`](#foreachright) * [`groupBy`](#groupby) * [`head`](#head) * [`indexOfAll`](#indexofall) @@ -813,6 +814,32 @@ flattenDepth([1, [2], 3, 4]); // [1,2,3,4]
[⬆ Back to top](#table-of-contents) +### forEachRight + +Executes a provided function once for each array element, starting from the array's last element. + +Use `Array.slice(0)` to clone the given array, `Array.reverse()` to reverse it and `Array.forEach()` to iterate over the reversed array. + +```js +const forEachRight = (arr, callback) => + arr + .slice(0) + .reverse() + .forEach(callback); +``` + +
+Examples + +```js +forEachRight([1, 2, 3, 4], val => console.log(val)); // '4', '3', '2', '1' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### groupBy Groups the elements of an array based on the given function. diff --git a/docs/index.html b/docs/index.html index aa57b64ac..f0eda0fbc 100644 --- a/docs/index.html +++ b/docs/index.html @@ -40,7 +40,7 @@ },1700); } }, 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 ]
@@ -116,6 +116,12 @@ Object.assig
     ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth(v, depth - 1) : v), [])
     : arr.reduce((a, v) => a.concat(v), []);
 
flattenDepth([1, [2], 3, 4]); // [1,2,3,4]
+

forEachRight

Executes a provided function once for each array element, starting from the array's last element.

Use Array.slice(0) to clone the given array, Array.reverse() to reverse it and Array.forEach() to iterate over the reversed array.

const forEachRight = (arr, callback) =>
+  arr
+    .slice(0)
+    .reverse()
+    .forEach(callback);
+
forEachRight([1, 2, 3, 4], val => console.log(val)); // '4', '3', '2', '1'
 

groupBy

Groups the elements of an array based on the given function.

Use Array.map() to map the values of an array to a function or property name. Use Array.reduce() to create an object, where the keys are produced from the mapped results.

const groupBy = (arr, func) =>
   arr.map(typeof func === 'function' ? func : val => val[func]).reduce((acc, val, i) => {
     acc[val] = (acc[val] || []).concat(arr[i]);
diff --git a/snippets/forEachRight.md b/snippets/forEachRight.md
index 15f205209..cfda33a56 100644
--- a/snippets/forEachRight.md
+++ b/snippets/forEachRight.md
@@ -5,9 +5,13 @@ Executes a provided function once for each array element, starting from the arra
 Use `Array.slice(0)` to clone the given array, `Array.reverse()` to reverse it and `Array.forEach()` to iterate over the reversed array.
 
 ```js
-const forEachRight = (arr,callback) => arr.slice(0).reverse().forEach(callback);
+const forEachRight = (arr, callback) =>
+  arr
+    .slice(0)
+    .reverse()
+    .forEach(callback);
 ```
 
 ```js
-forEachRight([1,2,3,4], val => console.log(val)); // '4', '3', '2', '1'
+forEachRight([1, 2, 3, 4], val => console.log(val)); // '4', '3', '2', '1'
 ```