0
# lodash.foreachright
1
2
lodash.foreachright is a standalone modularized lodash method that provides right-to-left iteration over collections. It exports the `forEachRight` function which iterates over elements of arrays and objects from right to left, invoking an iteratee function for each element.
3
4
## Package Information
5
6
- **Package Name**: lodash.foreachright
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.foreachright`
10
11
## Core Imports
12
13
```javascript
14
const forEachRight = require('lodash.foreachright');
15
```
16
17
ES6 module syntax:
18
19
```javascript
20
import forEachRight from 'lodash.foreachright';
21
```
22
23
## Basic Usage
24
25
```javascript
26
const forEachRight = require('lodash.foreachright');
27
28
// Iterate over array from right to left
29
forEachRight([1, 2, 3], function(value) {
30
console.log(value);
31
});
32
// => logs '3', '2', '1' (in that order)
33
34
// With index parameter
35
forEachRight(['a', 'b', 'c'], function(value, index) {
36
console.log(index + ': ' + value);
37
});
38
// => logs '2: c', '1: b', '0: a'
39
40
// Object iteration (right to left key order)
41
forEachRight({'x': 1, 'y': 2}, function(value, key) {
42
console.log(key + ': ' + value);
43
});
44
// => logs 'y: 2', 'x: 1'
45
46
// Method chaining support
47
const result = forEachRight([1, 2, 3], function(value) {
48
console.log(value);
49
});
50
// Returns the original array [1, 2, 3] for chaining
51
```
52
53
## Capabilities
54
55
### Right-to-Left Collection Iteration
56
57
Iterates over elements of collection from right to left, invoking iteratee for each element. This method is like `forEach` except that it iterates over elements from right to left.
58
59
```javascript { .api }
60
/**
61
* Iterates over elements of collection from right to left, invoking iteratee for each element.
62
* @param {Array|Object} collection - The collection to iterate over
63
* @param {Function} [iteratee=identity] - The function invoked per iteration
64
* @returns {Array|Object} Returns the original collection for chaining
65
*/
66
function forEachRight(collection, iteratee);
67
```
68
69
**Parameters:**
70
- `collection` *(Array|Object)*: The collection to iterate over. Can be arrays, array-like objects, or plain objects.
71
- `[iteratee=identity]` *(Function)*: The function invoked per iteration. Receives three arguments:
72
- `value`: The current element value
73
- `index` (arrays) or `key` (objects): The current element index or property key
74
- `collection`: The entire collection being iterated
75
76
**Returns:**
77
- *(Array|Object)*: Returns the original `collection` unchanged to support method chaining
78
79
**Behavior:**
80
- For arrays: Iterates from the last index to the first index (length-1 to 0)
81
- For objects: Iterates over own enumerable properties in reverse key order
82
- Early termination: If iteratee explicitly returns `false`, iteration stops
83
- Undefined collections: Gracefully handles null/undefined without throwing errors
84
- Non-function iteratees: Automatically converted to appropriate function or uses identity function
85
86
**Usage Examples:**
87
88
```javascript
89
// Basic array iteration
90
forEachRight([1, 2, 3], function(value) {
91
console.log('Value:', value);
92
});
93
// => logs 'Value: 3', 'Value: 2', 'Value: 1'
94
95
// Array with index access
96
forEachRight(['apple', 'banana'], function(value, index, array) {
97
console.log(index + ': ' + value + ' (total: ' + array.length + ')');
98
});
99
// => logs '1: banana (total: 2)', '0: apple (total: 2)'
100
101
// Object property iteration
102
forEachRight({name: 'John', age: 30}, function(value, key, obj) {
103
console.log(key + ' = ' + value);
104
});
105
// => logs 'age = 30', 'name = John'
106
107
// Early termination with false return
108
forEachRight([1, 2, 3, 4], function(value) {
109
if (value === 2) return false; // Stop iteration
110
console.log(value);
111
});
112
// => logs '4', '3' only (stops when reaching 2)
113
114
// Chaining example
115
const numbers = [1, 2, 3];
116
const result = forEachRight(numbers, function(value) {
117
console.log(value * 2);
118
});
119
console.log(result === numbers); // => true (same reference returned)
120
```
121
122
## Types
123
124
For TypeScript usage with `@types/lodash.foreachright` or similar typing:
125
126
```typescript { .api }
127
/**
128
* Iteratee function signature for arrays
129
*/
130
type ArrayIteratee<T> = (value: T, index: number, array: T[]) => any;
131
132
/**
133
* Iteratee function signature for objects
134
*/
135
type ObjectIteratee<T> = (value: T, key: string, object: Record<string, T>) => any;
136
137
/**
138
* Main function overloads
139
*/
140
declare function forEachRight<T>(
141
collection: T[],
142
iteratee?: ArrayIteratee<T>
143
): T[];
144
145
declare function forEachRight<T>(
146
collection: Record<string, T>,
147
iteratee?: ObjectIteratee<T>
148
): Record<string, T>;
149
150
declare function forEachRight<T>(
151
collection: T[] | Record<string, T>,
152
iteratee?: Function
153
): T[] | Record<string, T>;
154
```
155
156
## Performance Notes
157
158
- **Optimized Array Iteration**: Uses efficient decrementing while loop for arrays
159
- **Object Iteration**: Leverages lodash's internal `baseForOwnRight` for consistent property ordering
160
- **Type Detection**: Fast `typeof` and `isArray` checks to choose optimal iteration strategy
161
- **Memory Efficient**: No intermediate array creation, operates directly on input collection
162
- **Early Exit**: Supports early termination to avoid unnecessary iterations
163
164
## Compatibility
165
166
- **Node.js**: Compatible with all Node.js versions supporting ES5+
167
- **Browsers**: Works in all modern browsers and IE 9+
168
- **Module Systems**: Supports CommonJS, AMD, and UMD module patterns
169
- **Lodash Ecosystem**: Follows lodash 4.x API conventions and type patterns
170
- **Method Chaining**: Returns original collection to support lodash chain operations