@kaelyx/ts-kit / array / sum
Function: sum()
sum(array, options?)
sum(
array,options?):number
Defined in: array/sum/sum.ts:26
Adds all the numbers in an array.
Parameters
array
readonly number[]
The numbers to add. An empty array gives initialValue.
options?
Optional. See SumNumberOptions.
Returns
number
initialValue plus the total of the numbers.
Example
sum([1, 2, 3]) // 6
sum([]) // 0
sum([1, 2, 3], { initialValue: 10 }) // 16sum<T>(array, options)
sum<
T>(array,options):number
Defined in: array/sum/sum.ts:43
Adds one number value from each object in an array.
Type Parameters
T
T extends object
Parameters
array
readonly T[]
The objects. An empty array gives initialValue.
options
See SumObjectOptions. key must be a key whose value is a number.
Returns
number
initialValue plus the total of the values at key.
Throws
If missingBehaviour is 'THROW' and an object has no value at key.
Example
sum([{ price: 2 }, { price: 3 }], { key: 'price' }) // 5
sum([{ price: 2 }, {}], { key: 'price', missingBehaviour: SumOptionsMissingBehaviour.zero }) // 2Types
SumKey<T> type
SumKey<
T> = { [K in keyof T]-?: [T[K]] extends [number | undefined] ? [T[K]] extends [undefined] ? never : K : never }[keyofT]
Defined in: array/sum/sum.types.ts:10
The keys of T that have a value of type number. An optional key is included. See missingBehaviour in SumObjectOptions for what happens when its value is missing.
Type Parameters
T
T
SumOptionsMissingBehaviour type
SumOptionsMissingBehaviour =
ValueOf<typeofSumOptionsMissingBehaviour>
Defined in: array/sum/sum.types.ts:19
What sum does when an object has no value at key.
SumNumberOptions interface
Defined in: array/sum/sum.types.ts:30
Options for sum when the array holds numbers.
Properties
initialValue?
readonlyoptionalinitialValue?:number
Defined in: array/sum/sum.types.ts:32
The value to start the total from.
Default
0key?
readonlyoptionalkey?:undefined
Defined in: array/sum/sum.types.ts:34
Not used for an array of numbers.
missingBehaviour?
readonlyoptionalmissingBehaviour?:undefined
Defined in: array/sum/sum.types.ts:36
Not used for an array of numbers.
SumObjectOptions<T> interface
Defined in: array/sum/sum.types.ts:40
Options for sum when the array holds objects.
Type Parameters
T
T
Properties
initialValue?
readonlyoptionalinitialValue?:number
Defined in: array/sum/sum.types.ts:42
The value to start the total from.
Default
0key
readonlykey:SumKey<T>
Defined in: array/sum/sum.types.ts:44
The key of the number to add from each object. See SumKey.
missingBehaviour?
readonlyoptionalmissingBehaviour?:"THROW"|"ZERO"
Defined in: array/sum/sum.types.ts:46
What the function does when the value at key is missing or undefined.
Default
SumOptionsMissingBehaviour.zero