array-splitter

Tiny library to split an array into multiple smaller arrays by distributing the elements evenly.

Usage no npm install needed!

<script type="module">
  import arraySplitter from 'https://cdn.skypack.dev/array-splitter';
</script>

README

array-splitter

A tiny library to split an array into multiple smaller arrays by distributing the elements evenly.

How to install

$ npm install --save array-splitter

How to use

const split = require('array-splitter');

split([1, 2, 3, 4, 5, 6, 7, 8, 9], 3);
// Returns -> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

split([1, 2, 3, 4, 5, 6, 7, 8, 9], 4);
// Returns -> [[1, 2, 3], [4, 5], [6, 7], [8, 9]]

split([1, 2, 3, 4, 5, 6, 7, 8, 9], 14);
// Returns -> [[1], [2], [3], [4], [5], [6], [7], [8], [9], [], [], [], [], []]

Documentation

split(items, bucketCount)

items

Required
Type: array

The array to split up. After the split this array will become empty!

bucketCount

Required
Type: number

It must be an integer number greater than zero. The returned array contains exactly bucketCount number of arrays (i.e. buckets). If the number of item in the items array is less than this number, some empty arrays will be returned.

License

MIT