繁体   English   中英

在循环中迭代上一个下一个

[英]iterating previous next in a loop

是否有一种很好的惯用方法来迭代 Coffeescript 中的数组,但同时访问循环中的当前项和下一项? 例如在 Python 中,您可以执行以下操作:

[f(current, next) for current, next in zip(a, a[1:])]

这样的事情怎么样?

array = ['a', 'b', 'c', 'd']

for value, index in array
 current = value
 next = if array[index+1] then array[index+1] else null
 alert "#{current} at #{index} #{next}"

我去了这个:

(f(a[i], a[i-1]) for i in [1..segment.length-1])

我想不出更好的办法。

暂无
暂无

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

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