javascript - 在 for...in 循环中重置迭代器

标签 javascript iterator continue for-in-loop

这是我用来绘制一些随机圆圈的代码的一部分:

if(circles.length != 0) { //1+ circles have already been drawn
  x = genX(radius);
  y = genY(radius);
  var i = 0;
  iCantThinkOfAGoodLabelName:
  for(i in circles) {
    var thisCircle = circles[i];
    if(Math.abs(x-thisCircle["x"])+Math.abs(y-thisCircle["y"])>radius*2) {
      //overlaps
    } else {
      //overlaps
      x = genX(radius);
      y = genY(radius);
      continue iCantThinkOfAGoodLabelName;
    }

    if(i == circles.length - 1) { //Last iteration
      //Draw circle, add to array
    }
  }
}

问题是,当存在重叠时,不会检查具有新生成坐标的圆与已经检查过重叠圆的圆是否重叠。我尝试在使用 continue 语句之前将 i 设置为 0,但这不起作用。请帮忙,我真的很困惑。

最佳答案

You should not use for ... in on arrays.

使用for(var i = 0; i < circles.length; ++i)反而。然后你可以通过设置 i = 0 来重置.

关于javascript - 在 for...in 循环中重置迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6393876/

相关文章:

c++ - 指向错误元素的列表迭代器

c++11 - 为什么我需要两次将迭代器解引用到智能指针而不是使用 operator->()?

python - Python 中的 for 循环中的 continue 和 pass 之间有区别吗?

php - 嵌套循环,打破内循环并继续主循环

python - “继续” 'for' 循环到前一个元素

javascript - CSS3 固定标题表干扰悬停元素

javascript - 为 Google 的 Prettify 创建自己的规则的最先进方法是什么?

JavaScript 异步功能

javascript - 使用 webpack 将 dist 复制到 libs 目录

c++ - 用 C++ 简化多项式中的项