Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coffeescript: Break out of a forEach loop [duplicate]

Possible Duplicate:
Javascript Array.forEach HowTo break?

Given that I have a forEach loop, how do I break out of that loop in case X is true?

For example:

user.forEach (x,i) ->

                    if x.status == "available"
                        -- I want to break here - 

Thanks

like image 773
donald Avatar asked Oct 17 '11 03:10

donald


1 Answers

Basically, don't. forEach is meant to be essentially "functional". You could break it with an exception, but exceptions should be used for "exceptional" conditions, not control flow.

If you mean to break, you don't mean a functional form, you mean an iteration. Use a for loop.

like image 195
Charlie Martin Avatar answered Nov 04 '22 11:11

Charlie Martin