我有一个对象团队和一个字符串sharelink
var teams = {
team1 : {
roster : []
},
team2 : {
roster : []
},
};
var sharelink = "localhost:5000/team1=9234&5678&9999&6585team2=15566";
我想通过分享链解析每个团队的子串由" t"并使用以下代码使用它:
for(var team in teams){
var start = sharelink.search(team) + team.length + 1;
var end = start + 1;
while(sharelink[end]){
if(sharelink[end] == "t"){
break;
}
end++;
}
console.log(sharelink.slice(start, end));
/*
returns "9234&5678&9999&6585" on the first iteration and
"15566" on the second and final time through just like I want it to
*/
}
当我只是为了获得team1' s substring
而只是使用while循环时,它也可以正常工作var start = sharelink.search("team1") + "team1".length + 1;
var end = start + 1;
while(sharelink[end] != "t"){
end++;
}
console.log(sharelink.slice(start, end));
//returns "9234&5678&9999&6585" just as intended
但是下面的代码进入了无限循环。
for(var team in teams){
var start = sharelink.search(team) + team.length + 1;
var end = start + 1;
while(sharelink[end] != "t"){
end++;
}
}
为什么?