Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterating previous next in a loop

Is there a nice idiomatic way to iterate an array in Coffeescript but accessing both the current and the next item inside the loop? For example in Python you can do:

[f(current, next) for current, next in zip(a, a[1:])]
like image 425
Elian Avatar asked Apr 17 '26 01:04

Elian


1 Answers

How about something like this?

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}"
like image 170
uglycode Avatar answered Apr 18 '26 15:04

uglycode