staticinteger
staticinteger

Reputation: 103

Index of coffeescript .each loop

How can I get the index of a CoffeeScript .each loop? I've searched everywhere and can't seem to find a solid answer. I know how I can do this with vanilla jQuery, but I can't figure out how to add the index argument to function() in CoffeeScript.

here is my code currently:

video_list_element = $('#video-list li')

video_list_element.each ->
    video_list_element.delay(100).animate({
        "top": "0"
}, 2000)

I'm trying to multiply the value inside of .delay() by the index of the .each loop

Thank you so much for your help, I really appreciate it!!!

Regards, Tim

Upvotes: 7

Views: 8193

Answers (1)

mz3
mz3

Reputation: 1344

Documentation for the jQuery .each() function is found here: http://api.jquery.com/each/

video_list_element = $('#video-list li')
video_list_element.each (index, element) ->
  element.delay(100 * index).animate "top": "0", 2000

In general (sans-jQuery), the way to get the index in a coffeescript for loop is:

array = ["item1", "item2", "item3"]
for value, index in array
  console.log index, value

Gives:

0 item1
1 item2
2 item3

Upvotes: 6

Related Questions