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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:42:42+00:00 2026-05-27T09:42:42+00:00

I have a json object like this: [{ thing: Top, data: { childs: [{

  • 0

I have a json object like this:

[{
    "thing": "Top",
    "data": {
        "childs": [{
            "thing": "a",
            "data": {
                "text": "sdfgdg1",
                "morestuff": {
                    "thing": "Top",
                    "data": {
                        "childs": [{
                            "thing": "a",
                            "data": {
                                "text": "sdfg2",
                                "morestuff": "",
                            }
                        },
                        {
                            "thing": "a",
                            "data": {
                                "text": "gfhjfghj3",
                                "morestuff": {
                                    "thing": "Top",
                                    "data": {
                                        "childs": [{
                                            "thing": "a",
                                            "data": {
                                                "text": "asdfsadf 2 4",
                                                "morestuff": {
                                                    "thing": "Top",
                                                    "data": {
                                                        "childs": [{
                                                            "thing": "a",
                                                            "data": {
                                                                "text": "asdfsadf 2 5",
                                                                "morestuff": {
                                                                    "thing": "Top",
                                                                    "data": {
                                                                        "childs": {
                                                                            "thing": "a",
                                                                            "data": {
                                                                                "text": "asdfsadf 2 6",
                                                                                "morestuff": "",
                                                                            },
                                                                            "data": {
                                                                                "text": "asdfsadf 2 6",
                                                                                "morestuff": "",
                                                                            }
                                                                        },
                                                                    }
                                                                },
                                                            }
                                                        }],
                                                    }
                                                },
                                            }
                                        }],
                                    }
                                },
                            }
                        }],
                    }
                },
            }
        },
        {
            "thing": "a",
            "data": {
                "text": "asdfasd1 2",
                "morestuff": {
                    "thing": "Top",
                    "data": {
                        "childs": [{
                            "thing": "a",
                            "data": {
                                "text": "asdfsadf 2 3",
                                "morestuff": "",
                            }
                        }],
                    }
                },
            }
        },
        {
            "thing": "a",
            "data": {
                "text": "dfghfdgh 4",
                "morestuff": "",
            }
        }],
    }
}]  

…and I’m trying to iterate through it and get a total count on the “text” objects.

I can’t seem to be able to get something recursive working.. I think I’m missing a base-level understanding of both json and recursion..

After a couple of days of variations on this:

count=0;
c2=0;
c3=0;
function ra(arr){
    //console.log(arr.data.morestuff)
    if(arr!==undefined && arr.data && arr.data.morestuff===""){
        c3++;

    }else if((arr && arr.data && typeof arr.data.morestuff==="object")){
            if(arr.data.morestuff.data.childs.length>1){
                for(var w=0;w<arr.data.morestuff.data.childs.length;w++){
                    count+=ra(arr.data.morestuff.data.childs[w])
                }
            }else{
                count+=ra(arr.data.morestuff.data.childs[0])
            }
    }
         return(c3)
}
countn=0;//top morestuff with no morestuff
tot=0;
function reps(obj){
tot=obj.data.childs.length;
console.log("tot="+tot)
    for(var x=0;x<tot;x++){
        tot+=ra(obj.data.childs[x])
        c3=0
        if(tot>1000){//trying to prevent a runaway loop somehwere
            break;
        }
    }
    console.log(tot)
}

reps(json[0]); 

I’ve come to the conclusion that I just don’t know. I get all kinds of different results; some have come close by adding together the returns from the ra method, but nothing consistant (i.e. wrong) and always off by at least a few.

The JSON is consistent, though there are un-known numbers of children and childrens children which is why I’m looking to recursion.

Here is a fiddle: http://jsfiddle.net/CULVx/

Ideally, I’d like to count each text object, its’ relative position, and the number of children it has, but I figure I can mess with getting that stuff into an array if I could just get the counting working…

NB: I’ve tried jsonParse and other libraries to no avail. In particular, jsonParse throws an Object has no method "match" error when trying to use it on this json.

  • 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-27T09:42:43+00:00Added an answer on May 27, 2026 at 9:42 am

    If you just want all "text" properties at any depth, then this should be sufficient: http://jsfiddle.net/QbpqT/.

    You have a property key twice, though ("data" in the most nested object). Since an object cannot contain two properties with the same key, so you actually have 9 "text" properties; not 10.

    var count = 0;
    
    function iterate(obj) {
        for(var key in obj) { // iterate, `key` is the property key
            var elem = obj[key]; // `obj[key]` is the value
    
            if(key === "text") { // found "text" property
                count++;
            }
    
            if(typeof elem === "object") { // is an object (plain object or array),
                                           // so contains children
                iterate(elem); // call recursively
            }
        }
    }
    
    iterate(data); // start iterating the topmost element (`data`)
    
    console.log(count); // 9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function to sort a JSON object that looks like this: function
say I have the following json object; {'fname':'john', 'lname':'Locke'} and the following text boxes
I have an AJAX application that downloads a JSON object and uses the data
I have an object in Javascript that looks like this function MyObject(){ this.name=; this.id=0;
I have a JSON string (from PHP's json_encode() that looks like this: [{id: 1,
I have an array of json objects like so: [{a:b},{c:d},{e:f}] What is the best
I have a json object collection of geo locations that I build in the
Hi I have a JSON object that is a 2-dimentional array and I need
I have created a json object from ruby with cobravsmongoose, however the attributes have
I have the following JSON object: { response: { status: 200 }, messages: [

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.