@trenskow/group

    0.0.11 • Public • Published

    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 */);
    Parameter Description
    array The array that needs to be grouped.
    tester A 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).

    Keywords

    Install

    npm i @trenskow/group

    DownloadsWeekly Downloads

    14

    Version

    0.0.11

    License

    MIT

    Unpacked Size

    5.79 kB

    Total Files

    8

    Last publish

    Collaborators

    • trenskow