Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 619
staticinteger Avatar asked Sep 04 '14 15:09

staticinteger


1 Answers

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
like image 97
mz3 Avatar answered Oct 15 '22 22:10

mz3