【问题标题】:javascript recursive function with arrays only processes firs leg带有数组的javascript递归函数只处理第一站
【发布时间】:2023-03-08 09:30:01
【问题描述】:

这似乎是一个基本而微不足道的问题,但我无法弄清楚为什么某些内部循环没有得到处理。我有一个对象数组。我在对象内部调用带有 name 变量的函数,然后在数组中找到对象的索引。如果对象有孩子,然后我通过递归调用每个孩子的名字的函数来处理所有的孩子。这种情况一直持续到对象没有子对象为止。

结果是它只向下钻取初始对象的第一个子对象(在我的示例中它有七个),而不处理其他六个。在第一个中,它一直完成到最后(底层有八个孩子,他们都被处理了。

    function modifyCheckBox(thisName, itemChecked) {
        if (!itemChecked) itemChecked = 
            document.getElementById(thisName).checked;
            //find the position in the array for thie object
            var inputIndex = getIndex(thisName, checkBoxArray);
            alert("Processing "+thisName+" with inputIndex of "+inputIndex);
            if (checkBoxArray[inputIndex].hasChildren) {
                childArray = getChildren(checkBoxArray[inputIndex].name);
                childArrayString = "";
                for (k=0; k<childArray.length; k++) {
                    childArrayString += childArray[k].name + "  ";
                }
                alert ("Processing children " + childArrayString + " of " +
                    checkBoxArray[inputIndex].name + " with inputIndex=" + 
                    inputIndex);
                for (j=0; j<childArray.length; j++) {
                    //Repeat the process for this child
                    modifyCheckBox(childArray[j].name, itemChecked);
                }
                alert("Completed children of " + 
                      checkBoxArray[inputIndex].name);
            } else {
                alert(checkBoxArray[inputIndex].name + " has no children");
            }
       }

警报的输出列表是

    Processing chk_AINF with inputIndex of 5
    Processing children chk_TRK  chk_LEL  chk_FAST  chk_RW  chk_ARSRV  
                chk_SIG  chk_AMW of chk_AINF with inputIndex=5
        Processing chk_TRK with inputIndex of 6
            Processing children chk_TRK_0  chk_TRK_1  chk_TRK_2  chk_TRK_3  
                    chk_TRK_4 chk_TRK_5    chk_TRK_6  chk_TRK_7  of chk_TRK 
                    with  inputIndex=6
                Processing chk_TRK_0 with inputIndex of 50
                chk_TRK_0 has no children
                .......
                chk_TRK_7 has no children
            Completed children of chk_TRK
   Completed children of chk_AINF

我的问题是 chk_LEL chk_FAST chk_RW chk_ARSRV chk_SIG 和 chk_AMW,其余最上面的孩子发生了什么?

【问题讨论】:

  • 我想知道它是否将您的一些局部变量视为全局变量(因为您没有使用var 声明它们) - 因此当 modifyCheckbox 在子节点上运行时,它会更改父节点的循环计数器.我会尝试使用 var 关键字声明 childArray、childArrayString、j 和 k。

标签: javascript arrays recursion


【解决方案1】:

这不是使用递归的正确方法:

           for (j=0; j<childArray.length; j++) {
                //Repeat the process for this child
                modifyCheckBox(childArray[j].name, itemChecked);
            }

您不断将j 重新初始化为零,因此它只会查看第一个分支。尝试在函数外部初始化j(使用var 关键字),然后在函数内部递增它。然后你可以在你认为合适的时候递归,直到你的子元素用完为止。

【讨论】:

  • 我不确定我明白了。你在说什么。但是,所做的是将 for 循环更改为 childArray.forEach(function(child){ modifyCheckBox(child.name, itemChecked); });
【解决方案2】:

jmargolisvt,我不确定我是否明白你在说什么。但是,使它起作用的方法是将 for 循环更改为:

    childArray.forEach(function(child){
        modifyCheckBox(child.name, itemChecked);
    });

感谢您指出问题所在。

我删除了它,因为我在发布后认为它仍然不起作用。它确实奏效了。 (我发现其他问题使它看起来好像不起作用。)我相信这是最简单的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 2020-11-24
    • 2015-12-29
    • 1970-01-01
    • 2022-01-02
    • 2022-08-17
    • 1970-01-01
    相关资源
    最近更新 更多