sum-any-array

1.0.1 • Public • Published

round-discrete-distibution

Sum plain arrays and object arrays cause it can accept a second optional parameter that parse array value. So it accept a parser method so can accept number arrays [1, 4, 2, 3] and other type of content like object arrays

    [
      {
		....
    	"WHATEVER2": 1
      },
      ...
    ]

Install

npm install --save sum-any-array

Usage

sumArray

This method sums an array that could be an array of objects if parse function is provided.

const sumArray = require('sum-any-array');

const numberArray = [1, 4, 2, 3];

const sum = sumArray(numberArray);

console.log(sum);
/*
10
*/

Using object array:

const sumArray = require('sum-any-array');

const objArray = [
      {
        "name": "👨‍⚕️",
        "value": 1
      },
      {
        "name": "👩‍🏫",
        "value": 4
      },
	  {
        "name": "👨‍🍳",
        "value": 3
      },
      {
        "name": "👩‍🔧",
        "value": 2
      },
    ];
const getValue = (item) => item.value;

const sum = sumArray(objArray, getValue);

console.log(sum);
/*
10
*/

Using string array:

const sumArray = require('sum-any-array');

const strArray = ['a','b','ee'],;
const strLength = (str) => str.length;

const sum = sumArray(strArray, getValue);

console.log(sum);
/*
4
*/

License

MIT

Package Sidebar

Install

npm i sum-any-array

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

4.65 kB

Total Files

8

Last publish

Collaborators

  • jbarog