On the internet can not find the original method of the JS group, today has the time to write a method, feel also, take out to share, hope that you readers a lot of advice.
1$scope. GroupBy =function(itemlist, Gby) {2 3 varSetgroupobj =function(Noteobj,rule, Gby, Gindex, Maxindex)4 {5 varGname =Rule[gby[gindex]];6 if(Gindex = =Maxindex) {7 if(Noteobj[gname] = =undefined)8Noteobj[gname] = [];9 if(noteobj[gname].indexof (rule) < 0) {Ten noteobj[gname].push (rule); One } A } - Else { - if(Noteobj[gname] = =undefined) { theNoteobj[gname] = {}; - } -Setgroupobj (Noteobj[gname], rule, Gby, Gindex + 1, Maxindex); - } + } - + varNoteobj = {}; A for(vari = 0; i < itemlist.length; i++) { atSetgroupobj (Noteobj, Itemlist[i], Gby, 0, Gby.length-1); - } - - - varGetsubinfo =function(note, p, Gindex, Maxindex) { - varnewobj = {} inNewobj.key =p; -Newobj.value = []; to if(Gindex = =Maxindex) { + for(varKinchNote) { - Newobj.value.push (Note[k]); the } * } $ Else {Panax Notoginseng for(varKinchNote[p]) { -Newobj.value.push (Getsubinfo (Note[p][k], K, Gindex + 1, Maxindex)); the } + } A returnnewobj; the } + varMyObj = []; - for(varPinchnoteobj) { $Myobj.push (Getsubinfo (noteobj, p, 0, Gby.length-1)); $ } - returnmyobj; - } the$scope. grouprules = $scope. GroupBy (Allrules, [' Groupbycolumn1 ', ' groupbycolumn2 ']);
Where Allrules is the array to be grouped, Groupbycolumn1,groupbycolumn2 is the name of the field to be grouped and can support multiple.
The returned result is a grouped object:
var result={key: ", value:[
{key: ', Value:[]},{key: ', value:[]}
]}
JavaScript GROUP by groups data