【发布时间】:2015-08-27 11:04:57
【问题描述】:
是否有一种很好的惯用方式来迭代 Coffeescript 中的数组但同时访问循环内的当前项和下一项?例如在 Python 中你可以这样做:
[f(current, next) for current, next in zip(a, a[1:])]
【问题讨论】:
是否有一种很好的惯用方式来迭代 Coffeescript 中的数组但同时访问循环内的当前项和下一项?例如在 Python 中你可以这样做:
[f(current, next) for current, next in zip(a, a[1:])]
【问题讨论】:
我选择了这个:
(f(a[i], a[i-1]) for i in [1..segment.length-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}"
【讨论】: