The modern build of lodash's forEach utility function as a standalone module for iterating over collections.
npx @tessl/cli install tessl/npm-lodash--foreach@3.0.0Lodash 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.
npm install lodash.foreachvar forEach = require('lodash.foreach');ES6/ES2015:
import forEach from 'lodash.foreach';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 2Iterates 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 overiteratee (Function, optional): The function invoked per iteration. If not provided, the collection is still returned unchangedthisArg (*, optional): The this binding of iterateeReturns:
collection (not a copy)Behavior:
_.forIn or _.forOwn)false from iterateethisArg use optimized array iteration pathUsage 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)