简体   繁体   English

如何在循环内设置变量而不弄乱循环?

[英]How can I set a variable inside a loop without messing up the loop?

I'm quite new to this, so sorry if this is a daft question. 我对此很陌生,如果这是一个愚蠢的问题,请对不起。 I have searched for the answer but I can't find a close enough solution to adapt. 我已经搜索了答案,但是找不到足够接近的解决方案来适应。 This is part of my code: 这是我的代码的一部分:

for (var k = 0; k < (pointstoshow.length-1); k++){
   // k looping nicely at this point
u = pointstoshow[k]
   //k not looping at this point, only uses the last number
allpoints[u].setMap(map);
}

I am trying to access the numbers inside an array (pointstoshow) to use the number stored as a reference to search another array (allpoints), but I can't get it to work. 我正在尝试访问数组(pointstoshow)中的数字,以使用存储为参考的数字来搜索另一个数组(所有点),但是我无法使其正常工作。 If I set an alert to show pointstoshow[k] it works fine, it's only once I try and attach them to a variable it doesn't work. 如果我设置了一个警告以显示pointstoshow [k],则它可以正常工作,只有在尝试将它们附加到变量后,它才起作用。

I've been working on this most of today with no joy, so any suggestions appreciated. 我今天大部分时间都在工作,没有任何乐趣,因此欢迎提出任何建议。

Thanks 谢谢

You missed a semi-colon and you need to declare u. 您错过了分号,需要声明u。 Not entirely sure what needs to happen here so this is a shot in the dark. 不完全确定这里需要发生什么,因此这是黑暗中的一枪。

var u;
for(var k = 0; k < (pointstoshow.length-1); k++){  
    u = pointstoshow[k];  
    allpoints[u].setMap(map);  
}

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

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