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

    Function count

    • Counts the number of elements in an array that satisfy the given predicate function.

      统计数组中满足给定谓词函数的元素数量。

      Type Parameters

      • T

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

      Parameters

      • array: readonly T[]

        The array to count elements in. 要统计元素数量的数组

      • predicate: (item: T, index: number) => boolean

        The function to test each element. 用于测试每个元素的函数

      Returns number

      The number of elements that satisfy the predicate. 满足谓词的元素数量

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

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

      count([1, 2, 3, 4, 5], n => n > 2)
      // => 3
      count(['a', 'bb', 'ccc'], s => s.length > 1)
      // => 2