Component Each Iteration Utility
The `component-each` package provides a highly generalized utility function for iterating over arrays, objects, and strings. It was developed as part of the 'Component' project, an early modularity solution for JavaScript that predates modern package managers like npm for front-end assets. This library allows a single `each` function to handle different data types by inspecting their structure, making it a flexible but less performant alternative to native `forEach` or `for...in` loops. Its current stable version is 0.2.6, and it has not seen updates since 2013, indicating it is no longer actively maintained. Its primary differentiator was its role within the now largely superseded 'Component' ecosystem, rather than its unique iteration features which are now standard or handled more efficiently by native JavaScript or modern utility libraries like Lodash.
Common errors
-
Error: Cannot find module 'component-each'
cause The package was designed for the 'Component' build tool, not direct npm installation or standard Node.js module resolution without a bundler.fixEnsure `component-each` is properly installed and bundled by your build system. For modern projects, it's strongly recommended to replace it with native iteration or a modern utility library. -
TypeError: each is not a function
cause Attempting to import `each` using ESM `import` syntax (`import each from 'component-each'`) instead of CommonJS `require`.fixChange your import statement to `const each = require('component-each');`.
Warnings
- breaking This package is part of the 'Component' project ecosystem, which has been largely superseded by modern Node.js module systems (CommonJS/ESM) and build tools (npm, webpack, Rollup). It is not directly compatible with standard npm workflows without specific configuration or polyfills.
- gotcha The package is unmaintained since 2013. There will be no bug fixes, security updates, or new features. It may not work correctly with newer JavaScript runtimes or language features.
- gotcha Performance of this generic `each` function may be lower than native iteration methods (e.g., `Array.prototype.forEach`, `for...of` loops, `Object.keys().forEach()`), especially for large collections, due to its internal type checking and abstraction layer.
- gotcha This library does not provide TypeScript declarations and is not designed for TypeScript environments. Using it in a TypeScript project will require manual type declarations or `@ts-ignore` directives.
Install
-
npm install component-each -
yarn add component-each -
pnpm add component-each
Imports
- each
import each from 'component-each';
const each = require('component-each'); - each
import { each } from 'component-each';const each = require('component-each'); // Usage: each([1, 2, 3], (num, i) => console.log(num, i));
Quickstart
const each = require('component-each');
console.log('--- Iterating an array ---');
each([1, 2, 3], (num, i) => {
console.log(`Array item at index ${i}: ${num}`);
});
const config = {
name: 'example',
version: '1.0.0',
env: 'development'
};
console.log('\n--- Iterating an object ---');
each(config, (key, value) => {
console.log(`Object property '${key}': ${value}`);
});
console.log('\n--- Iterating a string ---');
each('hello', (char, i) => {
console.log(`String character at index ${i}: '${char}'`);
});