1

In coffeescript how can I have for loop withing a for loop

I am trying with following code but its not working

scale = filenames.length
for key, filename in filenames
    x = key
    for i in [x...scale]
        alert(i)

where filenames is an array of filenames

when I try following, it works

for key, filename in filenames
    x = key
    for i in [0...scale]
        alert(i)

1 Answer 1

3

In Coffeescript, the first argument passed to for ... in gets populated with the actual value, second, optional parameter, is for the index.

x = key is also not necessary in this code, you can directly refer to key.

The result should be as follows:

filenames = ['One.txt', 'Two.txt', 'Three.txt'] # a dummy array for testing purposes
scale = filenames.length

# now the actual loop
for filename, key in filenames
    for i in [key...scale]
        alert i

See second example in CoffeeScript - Loops and Comprehensions

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.