Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对象上的For循环正在失去控制_Javascript_Jquery_For Loop - Fatal编程技术网

Javascript 对象上的For循环正在失去控制

Javascript 对象上的For循环正在失去控制,javascript,jquery,for-loop,Javascript,Jquery,For Loop,我正在处理一个项目,它有一个疯狂的for循环来扩展D3.js交互画布中的节点。基本上,我想要的是扩大所有的孩子。所以,如果一个对象有一个子对象,我想展开它们 我从中删掉了一段代码。有这么多的for循环,太可笑了。我如何才能将其简化为一个简单的“查找所有子项,预成型切换();和更新();” $('.expandAll')。单击(函数(e){ e、 预防默认值(); 长度=root.children.length; 对于(变量i=0;i

我正在处理一个项目,它有一个疯狂的for循环来扩展D3.js交互画布中的节点。基本上,我想要的是扩大所有的孩子。所以,如果一个对象有一个子对象,我想展开它们

我从中删掉了一段代码。有这么多的for循环,太可笑了。我如何才能将其简化为一个简单的“查找所有子项,预成型切换();和更新();”

$('.expandAll')。单击(函数(e){
e、 预防默认值();
长度=root.children.length;
对于(变量i=0;i
听起来是递归的好例子:

$('.expandAll').click(function(e) {
    e.preventDefault();

    expandAll(root);
});

var expandAll = function (node) {
    toggle(node);
    update(node);

    // edit: if nodes with no children are lacking the children property
    if (!node.children) {
        return;
    }

    for (var i = 0, length = node.children.length; i < length; i++) {
        expandAll(node.children[i]);
    }
};
$('.expandAll')。单击(函数(e){
e、 预防默认值();
expandAll(根);
});
var expandAll=函数(节点){
切换(节点);
更新(节点);
//编辑:如果没有子节点的节点缺少children属性
如果(!node.children){
返回;
}
for(var i=0,length=node.children.length;i
我不确定什么是
切换
更新
的确切含义,但在调用
expandAll(root)之后,您可能只需要执行一个顶级的
更新
调用

使用!如果您只需要支持三个级别,可以为此引入一个计数器变量

$('.expandAll').click(function(e) {
    e.preventDefault();
    expandAll(root, root.children/*, 3*/);
}
function expandAll(root, children/*, lev*/) {
    if (!children/* || lev<=0 */) return;
    var length = children.length;
    for (var i = 0; i < length; i++) {
         toggle(children[i]);
         update(root);
         expandAll(root, children[i].children/*, lev-1*/);
    }
}
$('.expandAll')。单击(函数(e){
e、 预防默认值();
expandAll(root,root.children/*,3*/);
}
函数expandAll(根、子项/*、列夫*/){

如果(!children/*| | lev不确定这是否有帮助,但我一直在对嵌套对象执行以下操作:

object = {
    children: [], // array of similar objects with children property and update function
    update: function(data){
        // update this item
        this.data = data;

        // update each immediate child
        if (this.children)
            for (var i in this.children)
                this.children[i].update(data);
    }
};

// then you just call update on the first item:
object.update(data);
如果您遵循这种模式,而不是在根级别设置复杂的循环,那么您只需循环直接子级并调用它们的更新函数,然后该函数循环它们的子级并一直向下


我不是一个优秀的JS开发人员,只是前几天我在处理一些嵌套注释;)

它…它失去了控制!停止主反应器!…但是真的。为什么不只构建几个函数呢?递归!递归!递归!你知道递归函数是什么吗?它是递归的吗?这会导致堆栈溢出或者
无法访问未定义的异常上的“i”
异常?似乎叶节点没有子数组长度为0的ad。@Bergi听起来像是节点对象的问题,而不是循环:)
object = {
    children: [], // array of similar objects with children property and update function
    update: function(data){
        // update this item
        this.data = data;

        // update each immediate child
        if (this.children)
            for (var i in this.children)
                this.children[i].update(data);
    }
};

// then you just call update on the first item:
object.update(data);