Skip to main content

count

Counts the number of items in the supplied array, collection or enumerable object.

Signature

count<T>(
list: TList<T> | undefined,
fn?: (item: T) => boolean
): number

Parameters

NameTypeOptionalDescription
listT[] | undefinedNoSpecifies the array, collection or enumerable object to iterate through.
fn(item: T) => booleanYesSpecifies the optional truth function. Only items that pass the truth test are included in the count. The function should return a boolean value whether the item should be counted. If omitted all items are counted.

Return value

Returns the number of items that pass.

Example

import { count } from "@tripetto/builder";

count(["a", "b"]); // Returns `2`
count<number>([1, 2, 3], (nItem: number) => nItem > 1); // Returns `2`