Skip to main content
This release is 5 versions behind 1.33.0 — the latest version of @es-toolkit/es-toolkit. Jump to latest

@es-toolkit/es-toolkit@1.29.0-dev.958+1d1d0ee6
Built and signed on GitHub Actions

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
3 months ago (1.29.0-dev.958+1d1d0ee6)
function sumBy
sumBy(array:
ArrayLike<number>
| null
| undefined
): number

Computes the sum of the number values in array.

Examples

Example 1

sumBy([1, 2, 3]); // => 6 sumBy(null); // => 0 sumBy(undefined); // => 0

Parameters

array:
ArrayLike<number>
| null
| undefined
  • The array to iterate over.

Return Type

Returns the sum.

sumBy(array: ArrayLike<bigint>): bigint

Computes the sum of the bigint values in array.

Examples

Example 1

sumBy([1n, 2n, 3n]); // => 6n

Parameters

array: ArrayLike<bigint>
  • The array to iterate over.

Return Type

Returns the sum.

sumBy(array:
ArrayLike<unknown>
| null
| undefined
): unknown

Computes the sum of the values in array.

It does not coerce values to number.

Examples

Example 1

sumBy(["1", "2"]); // => "12" sumBy([1, undefined, 2]); // => 3

Parameters

array:
ArrayLike<unknown>
| null
| undefined
  • The array to iterate over.

Return Type

unknown

Returns the sum.

sumBy<T>(
array: ArrayLike<T>,
iteratee: (value: T) => number,
): number

Computes the sum of the number values that are returned by the iteratee function.

Examples

Example 1

sumBy([{ a: 1 }, { a: 2 }, { a: 3 }], object => object.a); // => 6

Type Parameters

  • The type of the array elements.

Parameters

array: ArrayLike<T>
  • The array to iterate over.
iteratee: (value: T) => number
  • The function invoked per iteration.

Return Type

Returns the sum.

sumBy<T>(
array: ArrayLike<T>,
iteratee: (value: T) => bigint,
): bigint | number

Computes the sum of the bigint values that are returned by the iteratee function.

NOTE: If the array is empty, the function returns 0.

Examples

Example 1

sumBy([{ a: 1n }, { a: 2n }, { a: 3n }], object => object.a); // => 6n sumBy([], (item: { a: bigint }) => item.a); // => 0

Type Parameters

  • The type of the array elements.

Parameters

array: ArrayLike<T>
  • The array to iterate over.
iteratee: (value: T) => bigint
  • The function invoked per iteration.

Return Type

Returns the sum.

Add Package

deno add jsr:@es-toolkit/es-toolkit

Import symbol

import { sumBy } from "@es-toolkit/es-toolkit/compat";

---- OR ----

Import directly with a jsr specifier

import { sumBy } from "jsr:@es-toolkit/es-toolkit/compat";

Add Package

npx jsr add @es-toolkit/es-toolkit

Import symbol

import { sumBy } from "@es-toolkit/es-toolkit/compat";

Add Package

yarn dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { sumBy } from "@es-toolkit/es-toolkit/compat";

Add Package

pnpm dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { sumBy } from "@es-toolkit/es-toolkit/compat";

Add Package

bunx jsr add @es-toolkit/es-toolkit

Import symbol

import { sumBy } from "@es-toolkit/es-toolkit/compat";