sum
优质
小牛编辑
136浏览
2023-12-01
Gets the sum of the numeric values in the sequence.
Signature
Sequence.sum = function(valueFn) { /*...*/ }
Sequence.sum = function sum(valueFn) { if (typeof valueFn !== "undefined") { return this.sumBy(valueFn); } return this.reduce(function(x, y) { return x + y; }, 0); }
Name | Type(s) | Description |
---|---|---|
valueFn | Function? | The function used to select the numeric values that will be summed up. |
returns | * | The sum. |
Examples
Lazy([]).sum() // => 0 Lazy([1, 2, 3, 4]).sum() // => 10 Lazy([1.2, 3.4]).sum(Math.floor) // => 4 Lazy(['foo', 'bar']).sum('length') // => 6