Tree convert a data type to another data type Tree (recursive)

Tree structure conversion. The first version of recursive temporarily not optimized.

Further written talk recursive rules:
1, determining the return condition
2, the condition determining recursive
3, operation data
4, the return value typically includes: top-level nodes
Cause (no return value here is the use of a reference parameter passing mechanisms, directly address operation, while the top-level node that is the first to pass parameters)

buildExtTree1: function (root,treeDatas) {
        if (treeDatas && treeDatas.length != 0){
            for (var i = 0,len = treeDatas.length;i<len;i++){
                if (!treeDatas[i].hasChildren && i==len-1){
                    var child = new Ext.tree.TreeNode({
                        text: treeDatas[i].text,
                        id: treeDatas[i].id,
                        leaf: treeDatas[i].leaf
                    });
                    root.appendChild(child);
                    return;
                } else {
                    var child = new Ext.tree.TreeNode({
                        text: treeDatas[i].text,
                        id: treeDatas[i].id
                        // leaf: treeDatas[i].leaf
                    });
                    root.appendChild(child);
                    this.buildExtTree1(child,treeDatas[i].children);
                }
            }
        }else {
            return;
        }
    }

Guess you like

Origin blog.csdn.net/weixin_33755847/article/details/90830026