编写一个嵌套的for循环

coffeescript-ing a nested for loop

本文关键字:嵌套 for 循环 一个      更新时间:2023-09-26

我在javascript中嵌套了这个for循环,我不能把它放进coffeescript。它似乎不支持这些类型的嵌套/依赖的for循环。

    for (index = 1; index < colorId.length; ++index) {
      for (index2 = 0; index2 < index; ++index2) {
        if(colorId[index] == colorId[index2]){
          colorId[index2] = (projects[index].id%9 + 1) * 10 + (projects[index].id%5)
        }
      }
    }
colorId=['x','y','z']
for index in [1...colorId.length]
  for index2 in [0...index]
    console.log index, index2

为我工作,生产:

1 0
2 0
2 1

for ... in ...构造中,在进入循环之前对数组的长度进行计算。因此,如果在处理过程中以某种方式更改数组的长度,它将无法像预期的那样工作。

对于这种事情,你应该用while代替。