4

How can I find out the total children count of each root node P in my JSON object. This is not a DOM structure. Considering P2 's children also children of P1 as P2 is child of P1.

P1-|-C1            (P1 children count 3)
   |-C2
   |-P2-|-C1       (P2 children count 5)
        |-C2
        |-P3-|-C1  (P3 children count 2)
             |-C2
        |-C3
        |-C4
        |-P4-|-C1   (P4 children count 2)
             |-C2

Thanks in advance.

4
  • I think you need first level children. See my answer below Commented Jan 16, 2013 at 7:23
  • For clarity, are we talking about DOM nodes here or just some arbitrary tree structure? Commented Jan 16, 2013 at 7:40
  • no this is not DOM nodes. This is some arbitrary JSON data that i need to parse. Commented Jan 16, 2013 at 7:59
  • Care to provide some example data for us to play with? Commented Jan 16, 2013 at 8:16

4 Answers 4

4

It's fairly simple without any library or custom code.

//Or whatever way you want to grab a reference to the DOM node
var p1 = document.getElementById('P1');

p1.getElementsByTagName('*').length

Maybe I misunderstood the question. Is this what you want?:

p1.childNodes.length
Sign up to request clarification or add additional context in comments.

Comments

4

Here is how you can do this using recursive Depth-first search:

function dfs(current, result) {
  var children = current.getChildren(),
      forEach = Array.prototype.forEach;
  result = result || {};
  result[current.id] = result[current.id] || 0;
  forEach.call(children, function (c) {
    result[current.id] += 1;
    dfs(c, result);
  });
  return result;
}
var result = dfs(document.getElementById('p1'));

console.log(result);

The script uses pure JavaScript.

Sample HTML:

<div id="p1">
  <div id="p2">
    <div id="p5"></div>
    <div id="p6"></div>
  </div>
  <div id="p3">
    <div id="p7"></div>
    <div id="p8"></div>
    <div id="p9"></div>
    <div id="p10">
        <div id="p11"></div>
    </div>
  </div>
  <div id="p4"></div>
</div>

result contains:

p1: 3
p2: 2
p3: 4
p4: 0
p5: 0
p6: 0
p7: 0
p8: 0
p9: 0
p10: 1
p11: 0

Note that you must have id of each element, otherwise you'll get results like:

undefined: 15
p1: 5
...

Here is an example in jsfiddle.

Comments

1

Here's a little recursive function, off the top of my head, though...

function recChildCount(node, level)
{
    level = (level || 0)++;
    var tree = {},
    i = 0;
    tree['level' + level] = {count: node.childNodes.length,
                             children: []};
    if (node.hasChildNodes() === false)
    {//dead end
        return tree;
    }
    for (i; i < node.childNodes.length; i++)
    {
        tree['level' + level].children.push(recChildCount(node.childNodes[i],level));
    }
    return tree;
}

This should return an object along the lines of:

{level1: {count: 123,
          level2: { count: 12,
                    children: [level3: { count: 16,
                                        children:[{level4:{count: 0,
                                                          children:[]},
                                                  {level4: {count: 0,
                                                            children:[]}]
                                        }
                               ]
                   }
           }
 }

Or something like that, you could add a delete tree.children; when there are no children, of course.
Again, this is just off the top of my head, so this code might need some fine-tuning, but I hope it's enough to help you on your way

Comments

-1

Use jquery plugin

You can use .length with just a descendant selector, like this:

If you have to use .children(), then it's like this:

var count = $("#yourId").children().length;

You want only first level, so use

$('#yourId > *').children().length;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.