array-reduce-sum

1.0.0 • Public • Published

array-reduce-sum

Package for summing numbers in an array.

  • Returns sum of numbers in an array.
  • Takes an optional second argument for initial value

Behavior similar to Array.prototype.reduce() accumulator pattern that takes value inputs in an array from left-to-right and reduces to a single value.

var sum = [0, 1, 2, 3].reduce(function(acc, val) {
  return acc + val;
}, 0);
// sum is 6
 

Installation

npm install array-reduce-sum --save

Syntax

var fn = require('array-reduce-sum');
fn(array, [initialValue]);     // initialValue optional
                               // initialValue should be a number not a string

Usage

var sumArray = require('array-reduce-sum');
 
sumArray([5]);
// => 5
 
sumArray([5,10]);
// => 15
 
// Coerces numbers as strings to behave like numbers
sumArray(["5", "10"]);
// => 15
 
// Optional second argument with initial value
sumArray([5,10], 20);
// => 35

Support

Find this package useful? Support it on GitHugz!

Dependencies (0)

    Dev Dependencies (1)

    Package Sidebar

    Install

    npm i array-reduce-sum

    Weekly Downloads

    1

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • alexemrie