diff --git a/snippets/forEachRight.md b/snippets/forEachRight.md new file mode 100644 index 000000000..15f205209 --- /dev/null +++ b/snippets/forEachRight.md @@ -0,0 +1,13 @@ +### 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); +``` + +```js +forEachRight([1,2,3,4], val => console.log(val)); // '4', '3', '2', '1' +``` diff --git a/tag_database b/tag_database index 974ca3601..8fad426f9 100644 --- a/tag_database +++ b/tag_database @@ -44,6 +44,7 @@ filterNonUnique:array flatten:array flattenDepth:array,recursion flip:adapter,function +forEachRight:array,function formatDuration:date,math,string,utility fromCamelCase:string functionName:function,utility