Skip to content

@serenity-is/corelib / groupBy

Function: groupBy()

groupBy<TItem>(items, getKey): GroupByResult<TItem>

Defined in: src/compat/arrays-compat.ts:94

Groups an array with keys determined by specified getKey() callback. Resulting object contains group objects in order and a dictionary to access by key. This is similar to LINQ's ToLookup function with some additional details like start index.

Type Parameters

TItem

TItem

Parameters

items

TItem[]

Array to group. Groups an array by keys derived from each element.

getKey

(x) => any

Callback returning the group key for an element; null/undefined is normalized to "".

Returns

GroupByResult<TItem>

A GroupByResult with byKey dictionary and inOrder array. Each group records its order, key, items, and start index.

Remarks

Similar to LINQ ToLookup with extra order/start metadata. Uses Object.create(null) so prototype keys are safe.

Deprecated

Kept as a Q.groupBy compat shim; for new code consider Map-based grouping or toGrouping.

Example

groupBy([{k:'a'}, {k:'b'}, {k:'a'}], x => x.k).inOrder.length; // 2