1

I have an array of objects like this:

[{
name: "Peter",
childs: [{
    name: "John",
    childs: [{
        name: "Joseph",
        childs: []
    }]
}]
},
{
    name: "Carl",
    childs: [{
        name: "Sam",
        childs: []
    }]
}]

Where each person can have children and each of these children can have any number of children and so on. Does anyone know a good way to perform this on underscore.js?

2 Answers 2

3

It seems like you're asking how to do tree traversal, but not anything unique to underscore.js. You can do a depth first traversal this way:

var children = [{name: "Peter", childs: [...]}, ...]
function traverseAndPrint(tree) {
    for (var i = 0; i < tree.length; i++) {
        console.log(tree[i].name);
        traverseAndPrint(tree[i].childs);
    }
}

If you need to perform an arbitrary action on each element, you could do the following:

function traverseAndCallFunction(tree, functionToCall) {
    for (var i = 0; i < tree.length; i++) {
        functionToCall(tree[i]);
        traverseAndPrint(tree[i].childs);
    }
}

where functionToCall is a function that takes a child object (according to your example)

I don't see why you'd need underscore.js to do tree traversal.

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

5 Comments

Objects do not have a length property. Unless I'm mistaken, the arbitrary object/array nesting pattern wouldn't be traversed by this.
The root element, and all childs elements are arrays. This isn't just arbitrary data, it's arbitrarily large data.
You're right, I misinterpreted the question. This is not a general solution for tree traversal, but Thiago wasn't asking for that, making this a fine answer.
I updated my answer to provide a more general solution as well.
Thanks for the answer. I was thinking in a solution that I could apply in a underscore.js templating view as I'm migrating the code to Backbone.js. If I can do that, I can "treat" the data before send it to the template. Thanks a lot!!!!
1

Sorry, I don't know how specifically in underscore, but it seems a simple recursive function would be easy enough for you.

var data = [{ 
   name: "Peter",
   childs: [{
       name: "John",
       childs: [{
           name: "Joseph",
           childs: []
       }]
   }, { 
       name: "Carl",
       childs: [{
           name: "Sam",
           childs: []
       }]
   }]
}];

function trav(data, fn) {
    data.forEach(function(item) {
        for (var p in item)
            if (p === "childs")
                trav(item[p], fn);
            else
                fn(item[p], p);
    });
}

trav(data, function(val, prop) {
    console.log(prop, val);
});

1 Comment

I've made a recursive function in the "old code". As I'm passing it all to Backbonejs using the underscore.js templating, I was thinking in a good way to do that on the template. Anyway, thanks for the answer!!!!

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.