@pengzhanbo/utils - v3.10.0
    Preparing search index...

    Function sumBy

    • Computes the sum of the values returned by an iteratee function for each element of an array.

      The iteratee is invoked for each element, and the returned numbers are added together. Returns 0 for an empty array.

      根据迭代函数计算数组中所有元素返回值之和。

      对每个元素调用迭代函数,将返回的数字累加求和。数组为空时返回 0

      Type Parameters

      • T

        The type of elements in the array / 数组元素的类型

      Parameters

      • arr: readonly T[]

        The array to sum. 要求和的数组

      • iteratee: (item: T) => number

        The function invoked per element, returning a number. 对每个元素调用的迭代函数,返回数字

      Returns number

      The sum of the iteratee results. Returns 0 for an empty array. 迭代函数返回值之和,数组为空时返回 0

      O(n) time complexity - single pass through the array

      O(n) 时间复杂度 - 单次遍历数组

      sumBy([{ p: 10 }, { p: 20 }], (o) => o.p)
      // => 30