JavaScript GROUP by groups data

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.