0
# lodash.foreachright
1
2
The modern build of lodash's `forEachRight` function exported as a standalone Node.js module. This utility provides right-to-left iteration over collections with optimized performance for arrays and support for early termination.
3
4
## Package Information
5
6
- **Package Name**: lodash.foreachright
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.foreachright`
10
- **Dependencies**: lodash._baseeachright, lodash._bindcallback, lodash.isarray
11
12
## Core Imports
13
14
```javascript
15
var forEachRight = require('lodash.foreachright');
16
```
17
18
For modern environments:
19
20
```javascript
21
const forEachRight = require('lodash.foreachright');
22
```
23
24
## Basic Usage
25
26
```javascript
27
const forEachRight = require('lodash.foreachright');
28
29
// Iterate over array from right to left
30
forEachRight([1, 2, 3], function(value) {
31
console.log(value);
32
});
33
// => logs 3, 2, 1
34
35
// Early termination on return false
36
forEachRight(['a', 'b', 'c'], function(value, index) {
37
console.log(value);
38
if (index === 1) return false;
39
});
40
// => logs 'c', 'b' (stops at index 1)
41
42
// With thisArg binding
43
forEachRight([1, 2, 3], function(value) {
44
console.log(this.prefix + value);
45
}, { prefix: 'Number: ' });
46
// => logs 'Number: 3', 'Number: 2', 'Number: 1'
47
```
48
49
## Capabilities
50
51
### Right-to-Left Collection Iteration
52
53
Iterates over elements of a collection from right to left, executing a provided function for each element. The function supports arrays, objects, and strings with optimized performance for arrays.
54
55
```javascript { .api }
56
/**
57
* Iterates over elements of collection from right to left
58
* @param {Array|Object|string} collection - The collection to iterate over
59
* @param {Function} [iteratee] - The function invoked per iteration
60
* @param {*} [thisArg] - The this binding of iteratee
61
* @returns {Array|Object|string} Returns collection
62
*/
63
function forEachRight(collection, iteratee, thisArg);
64
```
65
66
**Parameters:**
67
- `collection` (Array|Object|string): The collection to iterate over
68
- `iteratee` (Function, optional): The function invoked per iteration. Called with arguments `(value, index|key, collection)`. Defaults to identity function.
69
- `thisArg` (any, optional): The `this` binding of `iteratee`
70
71
**Returns:**
72
- (Array|Object|string): Returns the original `collection` unchanged
73
74
**Behavior:**
75
- **Arrays**: Iterates from highest index to 0, using optimized implementation when no `thisArg` is provided
76
- **Objects**: Iterates over enumerable properties in reverse order
77
- **Strings**: Iterates over characters from right to left
78
- **Early Termination**: Iteration stops immediately if `iteratee` returns `false`
79
- **Performance**: Uses specialized array implementation for better performance when possible
80
81
**Usage Examples:**
82
83
```javascript
84
const forEachRight = require('lodash.foreachright');
85
86
// Array iteration
87
const numbers = [10, 20, 30];
88
forEachRight(numbers, function(value, index, arr) {
89
console.log(`Index ${index}: ${value}`);
90
});
91
// => Index 2: 30, Index 1: 20, Index 0: 10
92
93
// Object iteration
94
const obj = { a: 1, b: 2, c: 3 };
95
forEachRight(obj, function(value, key, obj) {
96
console.log(`${key}: ${value}`);
97
});
98
// => c: 3, b: 2, a: 1
99
100
// String iteration
101
forEachRight('abc', function(char, index) {
102
console.log(`${index}: ${char}`);
103
});
104
// => 2: c, 1: b, 0: a
105
106
// Early termination
107
const items = ['first', 'second', 'third'];
108
forEachRight(items, function(item, index) {
109
console.log(item);
110
return index !== 1; // Stop when index is 1
111
});
112
// => logs 'third', 'second' (stops at index 1)
113
114
// With context binding
115
const context = { multiplier: 10 };
116
forEachRight([1, 2, 3], function(value) {
117
console.log(value * this.multiplier);
118
}, context);
119
// => logs 30, 20, 10
120
```
121
122
### Alias: eachRight
123
124
In the full lodash library, this function is also available under the alias `eachRight` (accessible as `_.eachRight`). However, this standalone module exports only the `forEachRight` function.
125
126
```javascript { .api }
127
// Note: In full lodash library context:
128
// _.eachRight(collection, iteratee, thisArg)
129
//
130
// This standalone module provides:
131
// forEachRight(collection, iteratee, thisArg)
132
```