0.0.23 • Published 2 months ago

@trenskow/group v0.0.23

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

group

A small JavaScript function for grouping items in an array.

Installation

npm install --save @trenskow/group
const group = require('@trenskow/group');

Usage

const grouped = group(/* array */, /* tester */);
ParameterDescription
arrayThe array that needs to be grouped.
testerA function that is used to test the arrays items. *

Test function takes in two items as parameters and should return true if they are in the same group.

Example

const animals = [{
	name: 'dog',
	type: 'mammal'
}, {
	name: 'goldfish',
	type: 'fish'
}, {
	name: 'crocodile',
	type: 'reptile'
}, {
	name: 'cat',
	type: 'mammal'
}, {
	name: 'guppy',
	type: 'fish'
}, {
	name: 'turtle',
	type: 'reptile'
}];

const animalsByType = group(animals, (item1, item2) => {
	return item1.type === item2.type;
});

/*
	Result:

	[
		[{
			name: 'dog',
			type: 'mammal'
		}, {
			name: 'cat',
			type: 'mammal'
		}],

		[{
			name: 'goldfish',
			type: 'fish'
		}, {
			name: 'guppy',
			type: 'fish'
		}],

		[{
			name: 'crocodile',
			type: 'reptile'
		}, {
			name: 'turtle',
			type: 'reptile'
		}]
	]

*/

License

MIT (see LICENSE).

0.0.23

2 months ago

0.0.22

3 months ago

0.0.20

4 months ago

0.0.21

4 months ago

0.0.19

4 months ago

0.0.13

10 months ago

0.0.14

10 months ago

0.0.15

9 months ago

0.0.16

8 months ago

0.0.17

7 months ago

0.0.18

7 months ago

0.0.12

1 year ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.3

4 years ago

0.0.4

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago