https://github.com/s-taylor/array-split
split an array into multiple arrays using a predicate, with results keyed within an object
https://github.com/s-taylor/array-split
array divide group segment split
Last synced: 2 months ago
JSON representation
split an array into multiple arrays using a predicate, with results keyed within an object
- Host: GitHub
- URL: https://github.com/s-taylor/array-split
- Owner: s-taylor
- License: mit
- Created: 2017-05-17T22:43:26.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-05-29T07:15:00.000Z (over 8 years ago)
- Last Synced: 2025-10-11T01:13:11.548Z (2 months ago)
- Topics: array, divide, group, segment, split
- Language: JavaScript
- Size: 32.2 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# array-split
## What does it do?
Splits an array into multiple arrays using a predicate function.
returns an object containing the results keyed based on the return value of the predicate function.
## How do I use it?
Example: Separate odd and even numbers
```js
const split = require('array-split');
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const result = split(numbers, (num) => {
if (num % 2 === 0) return 'even';
return 'odd';
});
```
This will return...
```js
{
even: [2, 4, 6, 8],
odd: [1, 3, 5, 7, 9]
}
```
## Anything else?
The predicate function only supports returning types...
`null`, `undefined`, `string` or `number`
- If you return `null` or `undefined` the value is dropped from the result set.
- If you return a `string` or a `number`, it is keyed in the results object (numbers are converted to strings)