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 7065037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:54:27+00:00 2026-05-28T04:54:27+00:00

I have been at this all day, trying to write a recursive function in

  • 0

I have been at this all day, trying to write a recursive function in Javascript to get the sizes. I need some help :/

Basically, my JSON object contains hierarchical categories/subcategories. I’d like to determine the size of each category/subcategory.

Here is a simple demonstration: http://pastehtml.com/view/bkpzzlabs.html

I’d like to take the JSON on the left and get the HTML on the right. Perhaps I’ve just been staring at this computer monitor for too long because for the life of me I can’t figure this out.

Thanks so much for your help.

  • 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-05-28T04:54:28+00:00Added an answer on May 28, 2026 at 4:54 am

    The right-hand side of your "demonstration" suggests that you don’t just want to recursively count the total number of endpoints (or "leafs") in your JSON; you want to know the recursive size for each category (non-leaf node) in the tree. The key here is that the nodes in between the leafs and the root don’t count.

    Update: I updated the function; read below.

    Try this:

    function parseTree(parent) {
        var hasNonLeafNodes = false;
        var childCount = 0;
        
        for (var child in parent) {
            if (typeof parent[child] === 'object') {
                // Parse this sub-category:
                childCount += parseTree(parent[child]);
                // Set the hasNonLeafNodes flag (used below):
                hasNonLeafNodes = true;
            }
        }
        
        if (hasNonLeafNodes) {
            // Add 'num_children' element and return the recursive result:
            parent.num_children = childCount;
            return childCount;
        } else {
            // This is a leaf item, so return 1:
            return 1;
        }
    }
    

    This function will modify the original object, so that a new key 'num_children' is added to each non-leaf node in the tree. The function returns the total number of leaf nodes in all of the parent‘s descendants. Note that it’s also perfectly possible to call this function on a subset of your object.

    Here is a live demo: http://jsfiddle.net/PPvG/CXXaB/

    Sorry about that! I had to do something else for a bit and, apparently, forgot what it was I was doing here… 😉

    The updated solution should count the addresses correctly, but keep in mind that arrays can’t have named keys (such as num_children). This is relevant in your example as e.g. "Pothole" is an array.

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

Sidebar

Related Questions

well I have been trying to get Nivo Slider to work almost all day.
Been trying to figure this one out all day. I have a large text
Have been struggling all day trying to make this simple example work using socket.io.
I have been trying to figure this out all day. I've searched SO and
I have been trying to do this all day. At the end i managed
I have been trying to solve this problem all day, I googled a lot,
I have been banging my head on this one all day. The C++ project
Hey all, i have been trying for a few minutes on this problem and
So, I have been trying to fix this for about two months. It all
I'm stuck on this and have been all day.. I'm still pretty new to

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.