Ampersand.js Each Utility

1.0.1 · abandoned · verified Wed Apr 22

amp-each is a lightweight JavaScript utility module, part of the ampersand.js project, designed to provide an iteration function similar to `Array.prototype.forEach` or `_.each` from Lodash. It allows iterating over both arrays and objects, applying a callback function to each element or property. The ampersand.js ecosystem, from which amp-each originates, was popular around 2015-2017 as a modular alternative to larger frameworks like Backbone.js, favoring CommonJS modules for client-side applications. However, the project appears to be largely unmaintained, with its core modules having last seen significant updates many years ago (e.g., ampersand-state last published 8 years ago from October 2017). This package, at version 1.0.1, should be considered abandoned, lacking recent updates, active development, or a clear release cadence. Its primary differentiator was being part of a "non-frameworky" collection of modules, offering granular control over application structure.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `amp-each` to iterate over arrays and objects, applying a callback function to each element or property, and passing a context object.

const each = require('amp-each');

// Example 1: Iterating over an array
const numbers = [1, 2, 3, 4, 5];
const doubledNumbers = [];

each(numbers, function (num, index) {
  doubledNumbers.push(num * 2);
  console.log(`Array item at index ${index}: ${num}`);
});
console.log('Doubled Numbers:', doubledNumbers);

// Example 2: Iterating over an object
const user = {
  name: 'Alice',
  age: 30,
  city: 'New York'
};

console.log('\nUser details:');
each(user, function (value, key) {
  console.log(`${key}: ${value}`);
});

// Example 3: Using 'this' context (if the function supports it, which 'amp-each' typically would)
const contextObject = { multiplier: 10 };
const multipliedNumbers = [];

each(numbers, function (num) {
  multipliedNumbers.push(num * this.multiplier);
}, contextObject);
console.log('Multiplied by context:', multipliedNumbers);

view raw JSON →