UNPKG

@extra-array/group

Version:

Breaks array keeping similar values together.

53 lines (38 loc) 1.63 kB
Breaks array keeping similar values together. [:package:](https://www.npmjs.com/package/@extra-array/group) [:smiley_cat:](https://github.com/orgs/nodef/packages?repo_name=extra-array) [:running:](https://npm.runkit.com/@extra-array/group) [:vhs:](https://asciinema.org/a/332060) [:moon:](https://www.npmjs.com/package/@extra-array/group.min) [:scroll:](https://unpkg.com/@extra-array/group/) [:newspaper:](https://nodef.github.io/extra-array/) [:blue_book:](https://github.com/nodef/extra-array/wiki/) > Similar: [cut], [split], [group]. > This is part of package [extra-array]. [extra-array]: https://www.npmjs.com/package/extra-array <br> ```javascript array.group(x, [fc], [fm]); // x: an array // fc: compare function (a, b) // fm: map function (v, i, x) ``` ```javascript const array = require("extra-array"); var x = [1, 2, 2, -2, -2, 4]; array.group(x); // [ [ 1 ], [ 2, 2 ], [ -2, -2 ], [ 4 ] ] array.group(x, (a, b) => Math.abs(a) - Math.abs(b)); // [ [ 1 ], [ 2, 2, -2, -2 ], [ 4 ] ] array.group(x, null, v => Math.abs(v)); // [ [ 1 ], [ 2, 2, -2, -2 ], [ 4 ] ] ``` <br> <br> ## References - [Data.List.groupBy: Haskell](https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-List.html#v:groupBy) - [List-Extra.group: elm](https://package.elm-lang.org/packages/elm-community/list-extra/7.1.0/List-Extra#group) - [List-Extra.groupWhile: elm](https://package.elm-lang.org/packages/elm-community/list-extra/7.1.0/List-Extra#groupWhile) [group]: https://github.com/nodef/extra-array/wiki/group [cut]: https://github.com/nodef/extra-array/wiki/cut [split]: https://github.com/nodef/extra-array/wiki/split