Code Monkey home page Code Monkey logo

reduce's Introduction

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.

reduce's People

Contributors

dependabot[bot] avatar ryanmorr avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.