Skip to content

cluster

Split a list into many lists of the given size

109 bytes

Usage

Given an array of items and a desired cluster size (n), returns an array of arrays. Each child array containing n (cluster size) items split as evenly as possible.

import * as
import _
_
from 'radashi'
const
const gods: string[]
gods
= [
'Ra',
'Zeus',
'Loki',
'Vishnu',
'Icarus',
'Osiris',
'Thor',
'Apollo',
'Artemis',
'Athena',
]
const
const clustered: string[][]
clustered
=
import _
_
.
cluster<string>(array: readonly string[], size?: number): string[][]
export cluster

Splits a single list into many lists of the desired size.

@seehttps://radashi.js.org/reference/array/cluster

@example

cluster([1, 2, 3, 4, 5, 6], 2)
// [[1, 2], [3, 4], [5, 6]]

@version12.1.0

cluster
(
const gods: string[]
gods
, 3)
// => [
// ['Ra', 'Zeus', 'Loki'],
// ['Vishnu', 'Icarus', 'Osiris'],
// ['Thor', 'Apollo', 'Artemis'],
// ['Athena']
// ]