/reduce

Array's reduce for all iterable and enumerable objects

Primary LanguageJavaScriptThe UnlicenseUnlicense

reduce

Version Badge License Build Status

Array's reduce for all iterable and enumerable objects

Install

Download the CJS, ESM, UMD versions or install via NPM:

npm install @ryanmorr/reduce

Usage

Choose the reduce function to iterate from left-to-right or the reduceRight function to iterate right-to-left. Provide the iterable/enumerable object as the first argument, the initial value as the second argument, and the reducer function as the third argument:

import { reduce, reduceRight } from '@ryanmorr/reduce';

// Supports arrays obviously
const array = ['1', '2', '3'];
const arrayValue = reduce(array, '', (accumulator, value, index, object) => {
    return accumulator + value;
});
console.log(arrayValue); // => "123"

// Supports iterables like maps, sets, nodelists, etc.
const set = new Set(['1', '2', '3']);
const setValue = reduce(set, '', (accumulator, value, index, object) => {
    return accumulator + value;
});
console.log(setValue); // => "123"

// Supports enumerable key/value objects
const object = {a: '1', b: '2', c: '3'};
const objectValue = reduceRight(object, '', (accumulator, [key, value], index, object) => {
    return accumulator + value;
});
console.log(objectValue); // => "321"

License

This project is dedicated to the public domain as described by the Unlicense.