CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lodash-foreach

The modern build of lodash's forEach utility function as a standalone module for iterating over collections.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Lodash ForEach

Lodash ForEach is a standalone module providing the modern build of lodash's _.forEach utility function (also known as _.each). It enables iteration over arrays, objects, and strings with support for early termination and custom binding context, offering optimized performance by detecting array types and using specialized iteration strategies.

Package Information

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

Core Imports

var forEach = require('lodash.foreach');

ES6/ES2015:

import forEach from 'lodash.foreach';

Basic Usage

var forEach = require('lodash.foreach');
// or import forEach from 'lodash.foreach';

// Array iteration
forEach([1, 2, 3], function(value, index, array) {
  console.log(value);
});
// => 1 2 3

// Object iteration  
forEach({ 'a': 1, 'b': 2 }, function(value, key, object) {
  console.log(key + ': ' + value);
});
// => a: 1 b: 2

// String iteration
forEach('abc', function(char, index, string) {
  console.log(char);
});
// => a b c

// With binding context
var context = { multiplier: 2 };
forEach([1, 2, 3], function(value) {
  console.log(value * this.multiplier);
}, context);
// => 2 4 6

// Early exit by returning false
forEach([1, 2, 3, 4, 5], function(value) {
  if (value === 3) return false;
  console.log(value);
});
// => 1 2

Capabilities

ForEach Iteration

Iterates over elements of collection invoking iteratee for each element. The iteratee is bound to thisArg and invoked with three arguments: (value, index|key, collection). Iteratee functions may exit iteration early by explicitly returning false.

/**
 * Iterates over elements of collection invoking iteratee for each element.
 * The iteratee is bound to thisArg and invoked with three arguments:
 * (value, index|key, collection). Iteratee functions may exit iteration 
 * early by explicitly returning false.
 * 
 * @param {Array|Object|string} collection - The collection to iterate over
 * @param {Function} [iteratee] - The function invoked per iteration
 * @param {*} [thisArg] - The this binding of iteratee  
 * @returns {Array|Object|string} Returns collection
 */
function forEach(collection, iteratee, thisArg);

Parameters:

  • collection (Array|Object|string): The collection to iterate over
  • iteratee (Function, optional): The function invoked per iteration. If not provided, the collection is still returned unchanged
  • thisArg (*, optional): The this binding of iteratee

Returns:

  • (Array|Object|string): Returns the exact same reference to the original collection (not a copy)

Behavior:

  • Objects with a "length" property are iterated like arrays (to avoid this behavior, use _.forIn or _.forOwn)
  • Iteratee receives three arguments: (value, index|key, collection)
  • Early exit is supported by explicitly returning false from iteratee
  • Performance optimization: Arrays without thisArg use optimized array iteration path
  • For all other cases, uses base collection iteration with callback binding
  • Non-function iteratees are handled gracefully (collection is returned unchanged)
  • Null or undefined collections are handled safely

Usage Examples:

var forEach = require('lodash.foreach');

// Basic array iteration with return value
var result = forEach([1, 2], function(n) {
  console.log(n);
});
// => logs each value from left to right and returns the array
// result equals [1, 2]

// Object iteration with guaranteed behavior
forEach({ 'user1': { 'active': true }, 'user2': { 'active': false } }, 
  function(user, key) {
    if (user.active) {
      console.log(key + ' is active');
    }
  }
);

// String character iteration
forEach('hello', function(char, index) {
  console.log('char at ' + index + ': ' + char);
});

// Early termination example
var found = false;
forEach([1, 2, 3, 4, 5], function(value) {
  if (value === 3) {
    found = true;
    return false; // Exit early
  }
  console.log('Processing: ' + value);
});
// => Processing: 1, Processing: 2

// Performance optimization - no thisArg binding for arrays
forEach(largeArray, function(item, index) {
  // Fast array iteration path
  processItem(item);
});

// Binding context for complex operations
var processor = {
  prefix: '[LOG]',
  process: function(items) {
    forEach(items, function(item) {
      console.log(this.prefix + ' ' + item);
    }, this);
  }
};

// Array-like objects with length property are treated as arrays
var arrayLike = { 0: 'a', 1: 'b', 2: 'c', length: 3 };
forEach(arrayLike, function(value, index) {
  console.log(index + ': ' + value);
});
// => 0: a, 1: b, 2: c

// Edge case handling - non-function iteratee
var result = forEach([1, 2, 3]); // No iteratee provided
console.log(result); // => [1, 2, 3] (original array returned)

// Edge case handling - null/undefined collections
forEach(null, function() {}); // => null (handled safely)
forEach(undefined, function() {}); // => undefined (handled safely)

// Demonstrating that original reference is returned
var original = [1, 2, 3];
var returned = forEach(original, function(n) { console.log(n); });
console.log(original === returned); // => true (same reference)
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.foreach@3.0.x
Publish Source
CLI
Badge
tessl/npm-lodash-foreach badge