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.
npm install lodash.foreachrightconst forEachRight = require('lodash.foreachright');ES6 module syntax:
import forEachRight from 'lodash.foreachright';const forEachRight = require('lodash.foreachright');
// Iterate over array from right to left
forEachRight([1, 2, 3], function(value) {
console.log(value);
});
// => logs '3', '2', '1' (in that order)
// With index parameter
forEachRight(['a', 'b', 'c'], function(value, index) {
console.log(index + ': ' + value);
});
// => logs '2: c', '1: b', '0: a'
// Object iteration (right to left key order)
forEachRight({'x': 1, 'y': 2}, function(value, key) {
console.log(key + ': ' + value);
});
// => logs 'y: 2', 'x: 1'
// Method chaining support
const result = forEachRight([1, 2, 3], function(value) {
console.log(value);
});
// Returns the original array [1, 2, 3] for chainingIterates 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.
/**
* Iterates over elements of collection from right to left, invoking iteratee for each element.
* @param {Array|Object} collection - The collection to iterate over
* @param {Function} [iteratee=identity] - The function invoked per iteration
* @returns {Array|Object} Returns the original collection for chaining
*/
function forEachRight(collection, iteratee);Parameters:
collection (Array|Object): The collection to iterate over. Can be arrays, array-like objects, or plain objects.[iteratee=identity] (Function): The function invoked per iteration. Receives three arguments:
value: The current element valueindex (arrays) or key (objects): The current element index or property keycollection: The entire collection being iteratedReturns:
collection unchanged to support method chainingBehavior:
false, iteration stopsUsage Examples:
// Basic array iteration
forEachRight([1, 2, 3], function(value) {
console.log('Value:', value);
});
// => logs 'Value: 3', 'Value: 2', 'Value: 1'
// Array with index access
forEachRight(['apple', 'banana'], function(value, index, array) {
console.log(index + ': ' + value + ' (total: ' + array.length + ')');
});
// => logs '1: banana (total: 2)', '0: apple (total: 2)'
// Object property iteration
forEachRight({name: 'John', age: 30}, function(value, key, obj) {
console.log(key + ' = ' + value);
});
// => logs 'age = 30', 'name = John'
// Early termination with false return
forEachRight([1, 2, 3, 4], function(value) {
if (value === 2) return false; // Stop iteration
console.log(value);
});
// => logs '4', '3' only (stops when reaching 2)
// Chaining example
const numbers = [1, 2, 3];
const result = forEachRight(numbers, function(value) {
console.log(value * 2);
});
console.log(result === numbers); // => true (same reference returned)For TypeScript usage with @types/lodash.foreachright or similar typing:
/**
* Iteratee function signature for arrays
*/
type ArrayIteratee<T> = (value: T, index: number, array: T[]) => any;
/**
* Iteratee function signature for objects
*/
type ObjectIteratee<T> = (value: T, key: string, object: Record<string, T>) => any;
/**
* Main function overloads
*/
declare function forEachRight<T>(
collection: T[],
iteratee?: ArrayIteratee<T>
): T[];
declare function forEachRight<T>(
collection: Record<string, T>,
iteratee?: ObjectIteratee<T>
): Record<string, T>;
declare function forEachRight<T>(
collection: T[] | Record<string, T>,
iteratee?: Function
): T[] | Record<string, T>;baseForOwnRight for consistent property orderingtypeof and isArray checks to choose optimal iteration strategy