R
fn

cluster

v0.0.3tested

Cluster an array into subarrays of a specific size

Examples

ts
cluster([1, 2, 3, 4, 5, 6, 7, 8], 3) // => [[1, 2, 3], [4, 5, 6], [7, 8]]
ts
cluster([1, 2, 3, 4], -1) // => []

Signature

ts
export function cluster<Value>(arr: Value[], size: number): Value[][]{ ... }

Type Parameters

Value

Parameters

ParameterTypeDescription
arrValue[]The array to cluster
sizenumberThe size of each cluster

Returns

Value[][]The clustered array