0.0.34 • Published 5 months ago

@trenskow/group v0.0.34

Weekly downloads
-
License
MIT
Repository
github
Last release
5 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.33

6 months ago

0.0.34

5 months ago

0.0.32

6 months ago

0.0.31

7 months ago

0.0.30

7 months ago

0.0.29

7 months ago

0.0.27

9 months ago

0.0.28

9 months ago

0.0.26

9 months ago

0.0.25

10 months ago

0.0.24

11 months ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.20

1 year ago

0.0.21

1 year ago

0.0.19

1 year ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.15

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.12

2 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.3

5 years ago

0.0.4

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago