101

GitHub - niksy/array-group-by-ponyfill: `Array.prototype.groupBy` ponyfill.

 2 years ago
source link: https://github.com/niksy/array-group-by-ponyfill
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

array-group-by-ponyfill

Array.prototype.groupBy ponyfill.

A proposal to make grouping of items in an array easier.

Install

npm install array-group-by-ponyfill --save

Usage

import groupBy from 'array-group-by-ponyfill';

groupBy([1, 2, 3, 4, 5], (value) => (value % 2 === 0 ? 'even' : 'odd'));

// { odd: [1, 3, 5], even: [2, 4] }

You can use named export preferNative if you wish to use native implementation if it’s available. In all other cases, ponyfill will be used. Beware of caveats!

groupBy(array, callback)

Returns: object

Group array items.

array

Type: any[]

Array to group.

callback

Type: Function

Callback which should return key with which to group array.

Argument Type Description

value any Current iteration value.

index number Current iteration index.

array any[] Original array reference.

Browser support

Tested in Chrome 72, Edge 15, Firefox 65 and should work in all modern browsers (support based on Browserslist configuration).

Test suite is taken and modified from core-js test suite.

For automated tests, run npm run test:automated (append :watch for watcher support).

License

MIT © Ivan Nikolić


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK