I have an array of objects. Each object looks like this:

```
{text: 'foo': level: N: children: M}

```

Both `N` and `M` are numbers. `N` represents how "deep" is located the object (think about the entire array as if it was a representation of a tree). `M` is the total number of children that the item has. **Total** means the sum of all nodes and leafs of all sub-levels.

The objects in the array that I have don't contain the `children` property and I must calculate it.

Example of "calculated" data:

```
{text: 'A': level: 0: children: 0}
{text: 'B': level: 0: children: 1}
{text: 'C': level: 1: children: 0}
{text: 'D': level: 0: children: 2}
{text: 'E': level: 1: children: 1}
{text: 'F': level: 2: children: 0}

A
B
|--C
D
|--E
   |--F

```

I could easily do a `for` loop, and then iterate one more time over the items, starting at the current position in the array and count the children (based on the level property), but I feel like this is not the best (as in "the fastest") way to do it.

How else could I do it?

Just to make sure nobody posts "worse" code than what I currently have, this is what I have:

```
for (var i = 0; i < data.length; i++) {
    var item = data[i];

    for (var j = i + 1; j < data.length; j++) {
        if (item.level < data[j].level) {
            item.children++;
        } else {
            break;
        }
    }
}

```