What is @ndhoule/each?
@ndhoule/each is a utility library for iterating over arrays and objects. It provides a simple and consistent way to perform operations on each element of a collection.
What are @ndhoule/each's main functionalities?
Iterate over arrays
This feature allows you to iterate over each element in an array, providing access to both the value and the index of each element.
const each = require('@ndhoule/each');
const array = [1, 2, 3, 4];
each((value, index) => {
console.log(`Index: ${index}, Value: ${value}`);
}, array);
Iterate over objects
This feature allows you to iterate over each property in an object, providing access to both the value and the key of each property.
const each = require('@ndhoule/each');
const object = { a: 1, b: 2, c: 3 };
each((value, key) => {
console.log(`Key: ${key}, Value: ${value}`);
}, object);
Other packages similar to @ndhoule/each
lodash
Lodash is a modern JavaScript utility library delivering modularity, performance, and extras. It provides a wide range of utility functions, including iteration methods like _.forEach, which can be used to iterate over arrays and objects similarly to @ndhoule/each.
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 function that can be used to iterate over arrays and objects, similar to @ndhoule/each.
async
Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. It includes an async.each function that can be used to iterate over collections asynchronously, offering more advanced control compared to @ndhoule/each.
each
Iterate over a collection, invoking a function for each element in the collection.
Installation
$ component install ndhoule/each
$ npm install @ndhoule/each
API
each(iterator : Function, collection : Array|Object|string) => undefined
Iterate over an input collection, invoking an iterator
function for each element in the collection, passing to the iterator three arguments: (value, index, collection)
.
The iterator
function can end iteration early by returning false
.
var log = console.log.bind(console);
each(log, ['a', 'b', 'c']);
each(log, 'tim');
each(log, { name: 'tim', occupation: 'enchanter' });
License
Released under the MIT license.