or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash-foreachright

The lodash method forEachRight exported as a module for right-to-left collection iteration.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.foreachright@4.4.x

To install, run

npx @tessl/cli install tessl/npm-lodash-foreachright@4.4.0

index.mddocs/

lodash.foreachright

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.

Package Information

  • Package Name: lodash.foreachright
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.foreachright

Core Imports

const forEachRight = require('lodash.foreachright');

ES6 module syntax:

import forEachRight from 'lodash.foreachright';

Basic Usage

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 chaining

Capabilities

Right-to-Left Collection Iteration

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.

/**
 * 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 value
    • index (arrays) or key (objects): The current element index or property key
    • collection: The entire collection being iterated

Returns:

  • (Array|Object): Returns the original collection unchanged to support method chaining

Behavior:

  • For arrays: Iterates from the last index to the first index (length-1 to 0)
  • For objects: Iterates over own enumerable properties in reverse key order
  • Early termination: If iteratee explicitly returns false, iteration stops
  • Undefined collections: Gracefully handles null/undefined without throwing errors
  • Non-function iteratees: Automatically converted to appropriate function or uses identity function

Usage 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)

Types

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>;

Performance Notes

  • Optimized Array Iteration: Uses efficient decrementing while loop for arrays
  • Object Iteration: Leverages lodash's internal baseForOwnRight for consistent property ordering
  • Type Detection: Fast typeof and isArray checks to choose optimal iteration strategy
  • Memory Efficient: No intermediate array creation, operates directly on input collection
  • Early Exit: Supports early termination to avoid unnecessary iterations

Compatibility

  • Node.js: Compatible with all Node.js versions supporting ES5+
  • Browsers: Works in all modern browsers and IE 9+
  • Module Systems: Supports CommonJS, AMD, and UMD module patterns
  • Lodash Ecosystem: Follows lodash 4.x API conventions and type patterns
  • Method Chaining: Returns original collection to support lodash chain operations