chunkBy es-toolkit
Splits an array into chunks of consecutive elements that share the same key.
Usage
chunkBy(arr, iteratee)
Walking left to right, each element's key is derived by iteratee. Whenever the key differs
from the previous element's key, a new chunk is started; otherwise the element is appended to
the current chunk. Keys are compared with !== (strict inequality), so equal primitives stay
together while distinct object references always start a new chunk.
Unlike chunk, which splits by a fixed size, chunkBy splits by a boundary
condition, keeping runs of same-keyed elements together. Reach for it when the grouping is
positional rather than global — collapsing repeated log levels, segmenting a timeline by
status, or batching sorted rows by their sort key.
Note that only consecutive runs are grouped. Non-adjacent elements sharing a key land in
separate chunks — use groupBy when you want a global grouping.
Parameters
The array to split into chunks.
A function that derives the comparison key for each element.
Returns
A two-dimensional array where each sub-array is a run of consecutive elements that produced the same key.
Examples
A data-last version for use with pipe is available from massaman/fp.
Source: es-toolkit
Re-exported verbatim from es-toolkit. Upstream has not published a reference page for chunkBy, so this page mirrors its source documentation instead.