Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8436919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:18:29+00:00 2026-06-10T07:18:29+00:00

I am looking for group by to perform at second level JSON string. For

  • 0

I am looking for group by to perform at second level JSON string. For example i am using the below code to use group by it works pefrectly fine where there is 1 level of array inside JSON string.

Array.prototype.groupBy = function(keyName) {
  var res = {};
  this.forEach(function(x) {
    var k = x[keyName];
    var v = res[k];
    if (!v) v = res[k] = [];
    v.push(x);
  });
  return res;
};

var myObject = {
      "groups": [{
            "id": "2",
            "name": "test group 1",
            "category": "clinical note",
            "author": "RRP"
        }, {
            "id": "1",
            "name": "test group 2",
            "category": "clinical image",
            "author": "LKP"
        }, {
            "id": "2",
            "name": "test group 1",
            "category": "clinical document",
            "author": "RRP"
        }, {
            "id": "4",
            "name": "test group 4",
            "category": "clinical note",
            "author": "John"
        }]

}

myObject.groups.sort(function(a,b) { return a.author > b.author } );    

var byJob = myObject.groups.groupBy('id');

for (var author in byJob) {
  document.write('<h3>'+author+','+byJob[author].length+'</h3>');  
  byJob[author].forEach(function(e) {  
    document.write('<p>'+e.id +', '+e.name+'</p>');
  });

Output::

1,1

1, test group 2

2,2

2, test group 1

2, test group 1

4,1

4, test group 4

Now the above example works perfectly fine but i am looking for second level array grouping in JSON string. For a example if i have following JSON string::

{
    "Category": {
        "old_modi":1,
        "new_modi":1,
        "FORM": [{
            "name":"form1",
            "date":"08/08/2012",
            "location":"abc",
            "PERSON":[{
                "ID":1,
                "author":"xyz"
            }]
        }]
    }
}

Would expect output (Author name would be group by its form name,date and location) ::

xyz
  form1 08/08/2012 xyz

Anybody have any idea which portion of the working copy which i posted in top need to change?

Thanks in advance..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:18:30+00:00Added an answer on June 10, 2026 at 7:18 am

    You can iterate through the javascript object recursively. One tricky thing is the javascript object can either be a json object or an array. In my sample solution, I had two different for-loop defined for each of them, there might be a easy way to do that. Anyway, my solution works for any level of groupby. here is the jsFiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for source code for a web based study group. I'd prefer something
I am looking to perform the following query (in pseudo-code) on Android: SELECT C.ID,
I'm looking to implement a chat room using PHP/Javascript (Jquery) with both group chat
I'm looking to extract, ID=(?P<group>.+?); from a string, the 'ID=' is a constant, group
I am looking to iterate over a group of divs and perform some random
I'm just looking to perform a LINQ query to return a group of course
I'm using C# on Framework 3.5. I'm looking to quickly group a Generic List<>
I have some code that is looking up group memberships from local groups on
Im looking to group and sort a Generic List<>. I have a list of
I'm looking for a way to group some ui elements together just like the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.