0.1.0 • Published 8 years ago
classify-by v0.1.0
classify-by
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
0.1.0
8 years ago