Search code examples
arraysindexingcoffeescriptframerjs

Get index value of array


Trying to do something simple but hitting a brick wall. I'm trying to get the index value of an item in an array, I'm using Coffeescript not plain Javascript.

Code:

for i in ["The Royal Family", "Residences", "History & Tradition", "News", "Events", "Jobs"]
    createSubMenuLayer(i, i.value)

I've tried i.index, i.value, plain old i (which gives me the string). I want to get the index value to position items based upon the position in the array.

Cheers.


Solution

  • The for x in ... form of the for loop iterates over the values of an array, not the indexes. But the documentation says:

    # Fine five course dining.
    courses = ['greens', 'caviar', 'truffles', 'roast', 'cake']
    menu i + 1, dish for dish, i in courses
    # -------------------^^^^^^^
    

    So you're looking for this:

    for e, i in [...]
        createSubMenuLayer(i, e)