@mirei/ts-collections
    Preparing search index...

    Function count

    • Counts the number of elements in the sequence, optionally restricted by a predicate.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      Parameters

      • source: Iterable<TElement>

        The source iterable.

      • Optionalpredicate: Predicate<TElement>

        Optional predicate that determines which elements are counted. When omitted, all elements are counted.

      Returns number

      The number of elements that satisfy the predicate.

      Prefer calling any(source) to test for existence instead of comparing this result with zero.

      const numbers = [1, 2, 3, 4, 5];
      const totalCount = count(numbers);
      console.log(totalCount); // 5

      const evenCount = count(numbers, x => x % 2 === 0);
      console.log(evenCount); // 2