I've got a collection of objects with the following format:
{
{
"articleId": "a0sf1000004najmAAA",
"articleName": "A value for name",
"footnote": "",
"mainCat": "A Category 1",
"subCat": "A SubCategory 1",
}, {
"articleId": "a0sf1000004najmAAA",
"articleName": "A value for name",
"footnote": "",
"mainCat": "A Category 1",
"subCat": "A SubCategory 2",
},
}
After reduction and grouping I would like to have the following:
{
"A Category 1": {
"A SubCategory 1": {
{
some items belonging to SubCat 1 and Cat 1
}
},
"A SubCategory 2": {
{
some items belonging to SubCat 2 and Cat 1
}
}
}
}
For now, I'm able to return all my single article grouped by sub-category, but how can I group my sub-categories by their main category?
var catalog = data;
var groupBySubCat = catalog.reduce(function(obj, item) {
obj[item.subCat] = obj[item.subCat] || [];
obj[item.subCat].push(item);
return obj;
}, {});
var groups = Object.keys(groupBySubCat).map(function(key) {
return {
subCat: key,
tests: groupBySubCat[key]
};
});
var maingroups = Object.keys(groupBySubCat).map(function(key) {
return {
subCat: key,
tests: groupBySubCat[key]
};
});
Any improvement would be appreciated.
sort... your code doesn't sort ...