javascript - Grouping a nested array with underscore.js -
sorry if seems duplicate, i've tried other answers , can't seem works properly. i'm building angularjs spa, , have data in following format:
"folders": [ { "id": 1, "password": "wpaugor452", "year": 2013 }, { "id": 2, "password": "wpaugor452", "year": 2013 }, { "id": 3, "password": "dfgoerj305", "year": 2014 }
and many many more.
i want folders grouped it's this:
"folders": [ "2013": [ "wpaugor452": [ { "id": 1, "password": wpaugor452, "year": 2013, }, { "id": 2, "password": wpaugor452, "year": 2013, } ] ], "2014": [ "dfgoerj305": [ { "id": 3, "password": "dfgoerj305", "year": 2014 } ] ] ]
there lot more real data, i've stripped down want group by. @ present, every folder has password , year, , want them grouped password within years, can display year in ui, , folders appropriate specific password.
although please note i'll want display year , password in ui (only once, grouped items below!)
if further detail needed, please ask.
this should want:
var result = _.chain(folders) .groupby('year') .mapobject( year => _.groupby(year, 'password')) .value();
and version full function definition:
var result = _.chain(folders) .groupby('year') .mapobject(function(year) { return _.groupby(year, 'password'); }) .value();
the solution first groups year , groups password each year group.
Comments
Post a Comment