classify-by

0.1.0 • Public • Published

classify-by NPM version NPM downloads Build Status

Classify an array by property and callback function.

Install

$ npm install --save classify-by

Usage

const classifyBy = require('classify-by')

const data = [
	{
		text: 'hello',
		date: '2015-09-01'
	},
	{
		text: 'world',
		date: '2016-01-10'
	},
	{
		text: 'hello',
		date: '2015-09-22'
	}
]

const result = classifyBy(data, 'date', function (date) {
	return new Date(date).getFullYear()
})

result.toObject()
/*
{
	'2015': [
		{text: 'hello', date: '2015-09-01'},
		{text: 'hello', date: '2015-09-22'}
	],
	'2016': [
		{text: 'world', date: '2016-01-10'}
	]
}
*/

result.toArray()
/*
[
	[
		{text: 'hello', date: '2015-09-01'},
		{text: 'hello', date: '2015-09-22'}
	],
	[
		{text: 'world', date: '2016-01-10'}
	]
]
*/

API

classifyBy(data, [prop, cb])

data

Type: array

prop

The key you wanna classify by.

cb

/**
 * @param value - data[index][prop]
 * @return the key name of each item in result
 */

License

MIT © EGOIST

Readme

Keywords

Package Sidebar

Install

npm i classify-by

Weekly Downloads

0

Version

0.1.0

License

MIT

Last publish

Collaborators

  • kchan