R
fn

sum

v0.0.3tested

Returns the sum of all the elements in an array

Example

ts
sum([1, 2, 3, 4, 5]) // => 15

sum([{ value: 1 }, { value: 2 }, { value: 3 }], (item) => item.value) // => 6

Signatures

ts
export function sum<Value extends number>(array: Value[]): number;
ts
export function sum<Value>(array: Value[], getValue: (item: Value) => number): number;

Type Parameters

Value

Parameters

ParameterTypeDescription
arrayValue[]The array to sum
getValue?((item: Value) => number) | undefinedA function that returns the value to sum from each element in the array

Returns

numberThe sum of all the elements in the array