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 with npm
npm i array-each --save
Usage
var each = require('array-each');
var result = [];
each(['a', 'b', 'c'], function (ele) {
result.push(ele + ele);
});
console.log(result);
Return false
to "break" early:
var result = [];
each(['a', 'b', 'c'], function (ele) {
if (ele === 'b') return false;
result.push(ele + ele);
});
console.log(result);
API
Loop over each item in an array and call the given function on every element.
Params
array
{Array}fn
{Function}thisArg
{Object}: Optionally pass a thisArg
to be used as the context in which to call the function.returns
{Array}
Example
each(['a', 'b', 'c'], function (ele) {
return ele + ele;
});
each(['a', 'b', 'c'], function (ele, i) {
return i + ele;
});
Related projects
- arr-filter: Faster alternative to javascript's native filter method.
- arr-diff: Returns an array with only the unique values from the first array, by excluding all… more
- arr-map: Faster, node.js focused alternative to JavaScript's native array map.
- arr-flatten: Recursively flatten an array or arrays. This is the fastest implementation of array flatten.
- array-unique: Return an array free of duplicate values. Fastest ES5 implementation.
- array-intersection: Return an array with the unique values present in all given arrays using strict equality… more
Running tests
Install dev dependencies:
npm i -d && npm test
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Author
Jon Schlinkert
License
Copyright (c) 2015 Jon Schlinkert
Released under the MIT license.
This file was generated by verb-cli on April 28, 2015.