Javascript循环遍历数组在中间退出
我有一个数组,我试图按 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此行看起来错误 -
parent_id
看起来像一个整数数组,因此id
变量需要是一个 int 才能通过此测试。然后,您可以在 if 语句的下一部分中将i
引用为数组 -id[i] != id
。This line looks wrong -
parent_id
looks like an array of ints so theid
variable would need to be an int to pass this test. You then refer toi
as an array in the next part of the if statement -id[i] != id
.看起来可能是这一行
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?一定有异常抛出。您应该添加异常处理(使用 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
在您的函数中,“id”指的是parent_id,因为这是您传递给函数的参数。
分解这一行意味着:
我建议重命名您的参数变量或 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:
I would suggest either renaming your argument variable, or the id array.
你的循环似乎运行了两次。我猜你正在为 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.