What is array-each?
The array-each npm package provides a simple utility for iterating over arrays and executing a function for each element. It is designed to be lightweight and efficient, making it suitable for scenarios where you need to perform operations on each item in an array.
What are array-each's main functionalities?
Basic Iteration
This feature allows you to iterate over each element in an array and execute a callback function. The callback receives the current value, the index, and the original array.
const each = require('array-each');
const array = [1, 2, 3, 4];
each(array, function(value, index, array) {
console.log('Value:', value, 'Index:', index);
});
Early Exit
This feature allows you to exit the iteration early by returning `false` from the callback function. This can be useful for performance optimization when you find the element you are looking for.
const each = require('array-each');
const array = [1, 2, 3, 4];
each(array, function(value, index, array) {
if (value === 3) return false; // Exit early
console.log('Value:', value, 'Index:', index);
});
Other packages similar to array-each
lodash
Lodash is a modern JavaScript utility library delivering modularity, performance, and extras. It provides a `_.forEach` method that offers similar functionality to array-each but with additional features and support for objects as well as arrays.
async
The async package provides higher-order functions and common patterns for asynchronous code. It includes an `async.each` method that allows for asynchronous iteration over arrays, which is useful for handling I/O-bound operations.
underscore
Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It includes an `_.each` method that works similarly to array-each but also supports objects.
array-each

Loop over each item in an array and call the given function on every element.
Install
Install with npm:
$ npm install --save array-each
Usage
Loop over each item in an array and call the given function on every element.
Params
array
{Array}fn
{Function}thisArg
{Object}: (optional) pass a thisArg
to be used as the context in which to call the function.returns
{undefined}
Example
each(['a', 'b', 'c'], function(ele) {
return ele + ele;
});
each(['a', 'b', 'c'], function(ele, i) {
return i + ele;
});
About
Related projects
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb
Running tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
Author
Jon Schlinkert
License
Copyright © 2017, Jon Schlinkert.
Released under the MIT License.
This file was generated by verb-generate-readme, v0.4.2, on February 26, 2017.