Skip to content

sumBy() function

각 요소에 f를 적용한 값들의 합을 반환합니다. 빈 iterable이면 0을 반환합니다.

Signature:

typescript
function sumBy<A>(f: (a: A) => number, iterable: Iterable<A>): number;
function sumBy<A>(
  f: (a: A) => number,
  iterable: AsyncIterable<A>,
): Promise<number>;

Example

ts
sumBy(
  (a) => a.price,
  [
    { name: "apple", price: 100 },
    { name: "banana", price: 200 },
  ],
); // 300

sumBy((a) => a.price, []); // 0

pipe(
  [{ price: 100 }, { price: 200 }],
  sumBy((a) => a.price),
); // 300

Open Source Code

Released under the Apache-2.0 License.