在 coffeescript 中,如何使用 for 循环进行 for 循环
我正在尝试使用以下代码,但它不起作用
scale = filenames.length
for key, filename in filenames
x = key
for i in [x...scale]
alert(i)
其中文件名是一个文件名数组
当我尝试跟随时,它起作用了
for key, filename in filenames
x = key
for i in [0...scale]
alert(i)
最佳答案
在 Coffeescript 中,传递给 for ... in
的第一个参数被填充为实际的 value
,第二个可选参数用于 index
。
x = key
在这段代码中也不是必须的,可以直接引用key
。
结果应该是这样的:
filenames = ['One.txt', 'Two.txt', 'Three.txt'] # a dummy array for testing purposes
scale = filenames.length
# now the actual loop
for filename, key in filenames
for i in [key...scale]
alert i
参见 CoffeeScript - Loops and Comprehensions 中的第二个示例
关于javascript - 在循环 coffeescript 中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31428492/