Group a set of objects by some specific semantic property
npm install groupbyGroup a set of objects by some specific semantic property.
``bash`
npm install --save groupby
`javascriptindex
const groupBy = require('groupby');
var data = [
{
index: '1',
name: 'foo'
},
{
index: '1.1',
name: 'bar'
},
{
index: '2.2',
name: 'hoo'
},
{
index: '3.1',
name: 'hia'
}
];
// by default it's grouped by `
data = groupBy(data);
// output
{
"1": {
"1": {
"name": "bar"
},
"name": "foo"
},
"2": {
"2": {
"name": "hoo"
}
},
"3": {
"1": {
"name": "hia"
}
}
}
It's useful for managing something with multi-level structure, for example a book, it has chapter 1, chapter 1.1 or even chapter 1.2.1. You can put all files in the same folder and use groupby` to structure them when you need the whole book index, like you want to build an API for the book.
MIT © EGOIST