1.0.2 • Published 3 years ago
@jswork/next-sum v1.0.2
next-sum
Gets the sum of the values in collection.
installation
npm install -S @jswork/next-sum
usage
import '@jswork/next-sum';
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [
{ key: '1', value: 111 },
{ key: '2', value: 222 },
{ key: '3', value: 333 },
{ key: '4', value: 444 }
];
const arr3 = [
{ key: 1, value: 111 },
{ key: 2, value: null },
{ key: 3, value: 2 },
{ key: 3, value: 'abc555' }
];
// simple
nx.sum(arr1); // 15
// use path(or xx.yy.zz)
nx.sum(arr2, 'value'); // 1110
// with callback
nx.sum(arr3, function (_, item) {
var val = parseInt(item.value, 10);
return val ? val : 0;
});
// 113
license
Code released under the MIT license.