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

  • SEARCH
  • Home
  • 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 8637621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:31:23+00:00 2026-06-12T10:31:23+00:00

I am trying to write a function in javascript that from JSON input data

  • 0

I am trying to write a function in javascript that from JSON input data returns the data belonging to a specific group, including the children of that group. The data looks like this:

            [
            {"id":"0", "name":"Person 0"},
            {"id":"1", "name":"Person 1","group":"0"},
            {"id":"2", "name":"Person 2","group":"0"},
            {"id":"3", "name":"Person 3","group":"2"},
            {"id":"4", "name":"Person 4","group":"2"},
            {"id":"5", "name":"Person 5","group":"4"},
            {"id":"6", "name":"Person 6","group":"4"},
            {"id":"7", "name":"Person 7","group":"0"},
            {"id":"8", "name":"Person 8","group":"7"}
            ]

Here, a person in group x belongs to the same group as person with id x.

For example: function(data, group) would return the following for function(data, 2):

            [
            {"id":"3", "name":"Person 3","group":"2"},
            {"id":"4", "name":"Person 4","group":"2"},
            {"id":"5", "name":"Person 5","group":"2"},
            {"id":"6", "name":"Person 6","group":"2"},
            ]

and function(data,0):

            [
            {"id":"1", "name":"Person 1","group":"1"},
            {"id":"2", "name":"Person 2","group":"2"},
            {"id":"3", "name":"Person 3","group":"2"},
            {"id":"4", "name":"Person 4","group":"2"},
            {"id":"5", "name":"Person 5","group":"2"},
            {"id":"6", "name":"Person 6","group":"2"},
            {"id":"7", "name":"Person 7","group":"7"},
            {"id":"8", "name":"Person 8","group":"7"}
            ]

I have tried to loop through the array but that doesn’t deal with the subgroups, so I guess I have to do it in a recursive fashion?

  • 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-12T10:31:24+00:00Added an answer on June 12, 2026 at 10:31 am

    You can .reduce() the data array and concatenate in the sub groups.

    function dataForGroup(data, group, refs) {
        if (!refs)
            refs = {};
    
           // check if it's in the list
        if (refs[group] === true)
            return []; // or you can throw an Error
        else
            refs[group] = true; // first encounter, so add it to the list
    
        return data.reduce(function(res, obj) {
            return obj.group == group ? 
                       res.concat(obj, dataForGroup(data, obj.id, refs)) : res;
        }, []);
    }
    

    Hopefully there are no circular references!

    Though I don’t understand the group(data, 0) output, and I don’t see any sub-group data.

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

Sidebar

Related Questions

I'm trying to write a javascript function that adds some DOM nodes to the
I am trying to write a javascript function with an if statement that will
I am trying write a function that generates simulated data but if the simulated
I am trying to write a javascript function that calls an Dictionary value and
I'm trying to write a Javascript function to edit content from clipboard before pasting.
gud day... i'm trying to write a program in javascript that uses .json. i
I'm trying to write a javascript function that calculates the time since Oct 11th,
I am trying to write a javascript function that when called performs function DoSomething()
I'm trying to write a function that will remove a query argument from a
I am trying to write a function that will allow me to browse for

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.