Javascript循环遍历数组在中间退出

发布于 2024-12-15 01:05:35 字数 1173 浏览 0 评论 0原文

我有一个数组,我试图按 array.length 循环,但它无缘无故地停在循环中间。

code :

    var id = ['88' , '89' , '90' , '91' , '92' , '93' , '94' , '95' , '96' , '97' , '98' ];
    var parent_id = ['1' , '1' , '88' , '1' , '88' , '91' , '93' , '93' , '92' , '90' , '97' ];

    function getAllLearningPaths(id){


    document.getElementById("catdiv").innerHTML += "THIS IS HE CURRENT CATEGORY ID : " + id     + "<br>"; 
    for(var i=0; i < id.length;i++)
    {
    document.getElementById("catdiv").innerHTML += "THIS IS HE CURRENT CATEGORY  PARENT    ID : " + parent_id[i] + "<br>"; 
    if(parent_id[i] == id && id[i] != id)
    {
      document.getElementById("catdiv").innerHTML += "I MADE IT!";
      getAllLearningPaths(parent_id[i]);


    }    
}

for(var i=0; i< Categories.length;i++)
{
    if(Categories[i] == id)
    {
        document.getElementById("l_ids_"+CategoriesValues[i]).checked = true;
       disablerow(document.getElementById("l_ids_"+CategoriesValues[i]), '1');


    }

}           

return;
}

类别填充在代码中的其他位置。没问题。 问题是第一个循环没有通过第二次运行。 此代码输出:

这是当前类别 ID:88 这是他当前的类别父 ID:1 这是他当前类别的家长 ID:1

知道吗?

i have an array i'm trying to loop through by array.length but it stops in the middle of the loop for no reason.

code :

    var id = ['88' , '89' , '90' , '91' , '92' , '93' , '94' , '95' , '96' , '97' , '98' ];
    var parent_id = ['1' , '1' , '88' , '1' , '88' , '91' , '93' , '93' , '92' , '90' , '97' ];

    function getAllLearningPaths(id){


    document.getElementById("catdiv").innerHTML += "THIS IS HE CURRENT CATEGORY ID : " + id     + "<br>"; 
    for(var i=0; i < id.length;i++)
    {
    document.getElementById("catdiv").innerHTML += "THIS IS HE CURRENT CATEGORY  PARENT    ID : " + parent_id[i] + "<br>"; 
    if(parent_id[i] == id && id[i] != id)
    {
      document.getElementById("catdiv").innerHTML += "I MADE IT!";
      getAllLearningPaths(parent_id[i]);


    }    
}

for(var i=0; i< Categories.length;i++)
{
    if(Categories[i] == id)
    {
        document.getElementById("l_ids_"+CategoriesValues[i]).checked = true;
       disablerow(document.getElementById("l_ids_"+CategoriesValues[i]), '1');


    }

}           

return;
}

categories is populated somewhere else in the code. it's no problem.
the thing is that the first loop dosen't pass the 2nd run.
this code outputs :

THIS IS HE CURRENT CATEGORY ID : 88
THIS IS HE CURRENT CATEGORY PARENT ID : 1
THIS IS HE CURRENT CATEGORY PARENT ID : 1

any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

柳若烟 2024-12-22 01:05:35

此行看起来错误 -

if(parent_id[i] == id && id[i] != id)

parent_id 看起来像一个整数数组,因此 id 变量需要是一个 int 才能通过此测试。然后,您可以在 if 语句的下一部分中将 i 引用为数组 - id[i] != id

This line looks wrong -

if(parent_id[i] == id && id[i] != id)

parent_id looks like an array of ints so the id variable would need to be an int to pass this test. You then refer to i as an array in the next part of the if statement - id[i] != id.

心的位置 2024-12-22 01:05:35

看起来可能是这一行 if(Categories[i] == id) 您正在对照 id 数组检查类别中的值,是否应该检查 id 内部的索引?

looks like it might be this line if(Categories[i] == id) you are checking a value in Categories agains the array of id, should you be checking against an index inside of id?

兮颜 2024-12-22 01:05:35

一定有异常抛出。您应该添加异常处理(使用 try catch)并使用 firebug 进行调试,而不是尝试找出代码中的问题所在

There must be exception thrown. Instead of trying to figure out where the problem is in your code, you should add exception handling (use try catch) and debug it with firebug

悲念泪 2024-12-22 01:05:35
if(parent_id[i] == id && id[i] != id)

在您的函数中,“id”指的是parent_id,因为这是您传递给函数的参数。

分解这一行意味着:

if(parent_id[i] == parent_id && parent_id[i] != parent_id)

我建议重命名您的参数变量或 id 数组。

if(parent_id[i] == id && id[i] != id)

In your function, 'id' refers to parent_id because that is the argument that you have passed to the function.

Breaking down this line means:

if(parent_id[i] == parent_id && parent_id[i] != parent_id)

I would suggest either renaming your argument variable, or the id array.

篱下浅笙歌 2024-12-22 01:05:35

你的循环似乎运行了两次。我猜你正在为 id 参数传递一个像“88”这样的字符串。因此它返回 id.length 2,因为 id 是一个字符串。

Your loop seems to be running twice. I guess you are passing in a string like '88' for id parameter. So it returns 2 for id.length since id is a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文