Classify an array by property and callback function.
MIT License
Classify an array by property and callback function.
$ npm install --save classify-by
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'}
]
]
*/
Type: array
The key you wanna classify by.
/**
* @param value - data[index][prop]
* @return the key name of each item in result
*/
MIT © EGOIST