array-divide

Divide an array into multiple arrays each of whom have the same number of items

Usage no npm install needed!

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

README

array-divide

npm version Build Status Coverage Status

Divide an Array into multiple Arrays each of whom have the same number of items

arrayDivide(['a', 'b', 'c', 'd'], 2); //=> [['a', 'b'], ['c', 'd']]
arrayDivide(['a', 'b', 'c', 'd'], 3); //=> [['a', 'b', 'c'], ['d']]

Installation

Use npm.

npm install array-divide

API

import arrayDivide from 'array-divide';

arrayDivide(array, count)

array: Array
count: number (the maximum number of items)
Return: Array (a new array and doesn't break the original one)

arrayDivide(['a', 'b', 'c', 'd'], 100); //=> [['a', 'b', 'c', 'd']]
arrayDivide([], 100); //=> []

arrayDivide(['a', 'b', 'c'], 0); // throw an error (count must be >= 1)
arrayDivide(new Set(), 1); // throw an error (doesn't support array-like object)

License

Copyright (c) 2015 - 2018 Shinnosuke Watanabe

Licensed under the MIT License.