简体   繁体   English

For循环陷入无限循环

[英]For loop stuck in an infinite loop

My for loop is stuck in an infinite loop and I can't figure out why. 我的for循环陷入无限循环,我不知道为什么。 Can someone tell me what I am doing wrong here? 有人可以告诉我我在做什么错吗?

jData is an array extracted from a text file. jData是从文本文件提取的数组。 The images are currently stored in a comma separated string and this loop should extract them and display them one by one. 图片目前以逗号分隔的字符串存储,此循环应提取它们并逐一显示。

  for( var i = 0 ; i < jData.length ; i++ ){ //runs loop to populate container. //console.log("array images contains " + jData[i].imgs); if (jData[i].imgs){ var sImages = jData[i].imgs; var aImages = sImages.split(','); for( var i = 0 ; i < aImages.length; i++) { sPropertyTemplate = sPropertyTemplate.replace( "{{images"+i+"}}" , aImages[i] ); console.log(aImages[i]); break; } } var sPropertyTemplate = sProperty; sPropertyTemplate = sPropertyTemplate.replace( "{{id}}" , jData[i].sUniqueId ); sPropertyTemplate = sPropertyTemplate.replace( "{{address}}" , jData[i].sAddress ); sPropertyTemplate = sPropertyTemplate.replace( "{{type}}" , jData[i].sType ); sPropertyTemplate = sPropertyTemplate.replace( "{{price}}" , jData[i].iPrice ); sPropertyTemplate = sPropertyTemplate.replace( "{{locLat}}" , jData[i].lat ); sPropertyTemplate = sPropertyTemplate.replace( "{{LocLng}}" , jData[i].lng ); 

You are using the same variable i in both, the inner and outer loop. 您在内部和外部循环中都使用了相同的变量i So you will always overwrite its value it should have in the outer for-loop. 因此,您将始终覆盖它在外部for循环中应具有的值。

What you should do, use a different variable for the inner loop, such as j : 您应该做的是,对内部循环使用其他变量,例如j

for( var j = 0 ; j < aImages.length; j++) {
    sPropertyTemplate = sPropertyTemplate.replace( "{{images"+j+"}}" , aImages[j] );
    console.log(aImages[j]);
    break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM