219

Possible Duplicate:
How to short circuit Array.forEach like calling break?

Is there a way so that I can break out of array map method after my condition is met ? I tried the following which throws "Illegal Break Statement" Error. This is some random example I came up with.

var myArray = [22,34,5,67,99,0];

var hasValueLessThanTen = false;

myArray.map(function (value){
    if(value<10){
      hasValueLessThanTen = true;
      break;
    }
  }
);

We can do using for loops, but I wanted to know whether we can accomplish the same using map method ?

7
  • 5
    It is in essence a duplicate of the question that Rob W refers to. However, the source of this misunderstanding is the wrong usage of map method, which is intended to, hmmm, map values, so it's not supposed to stop the iteration Commented Sep 4, 2012 at 9:36
  • 25
    @Yoni is correct. If the OP is looking for a breakable forEach loop, consider uing methods such as [].every (return true to continue, non-true to break) or [].some - return true to break, non-true to continue. Commented Sep 4, 2012 at 9:40
  • alternatively [].reduce. Passing [] as a second argument, and only modifying when conditions are met. Commented Aug 1, 2017 at 16:03
  • 5
    Also I don't understand why this was marked as a duplicate. The linked question is about forEach which has a much different solution. Commented Aug 1, 2017 at 16:06
  • 1
    @SandyGifford Agreed. Voting to reopen this question. Commented Jan 30, 2019 at 18:14

1 Answer 1

289

That's not possible using the built-in Array.prototype.map. However, you could use a simple for-loop instead, if you do not intend to map any values:

var hasValueLessThanTen = false;
for (var i = 0; i < myArray.length; i++) {
  if (myArray[i] < 10) {
    hasValueLessThanTen = true;
    break;
  }
}

Or, as suggested by @RobW, use Array.prototype.some to test if there exists at least one element that is less than 10. It will stop looping when some element that matches your function is found:

var hasValueLessThanTen = myArray.some(function (val) { 
  return val < 10;
});
Sign up to request clarification or add additional context in comments.

5 Comments

I appreciated the .some example even though Rob had already mentioned it. People don't always look in the comments, but typically atleast scan the code samples!
Why isn't it possible with the Array.prototype.map ?
@Vadorequest I did post the question when I was js newbie, Array.prototype.map was not supposed to be used that way at all, it is a helper method for a whole different usecase where you want to transform every element of a given array into a different variant. So, when it's going to be about "every element", you would never need a "break" statement, if you need a break statement it means maybe you don't need a map. I posted this question because I was trying to use that as a iterator, if you need iterator then go for the simple for loop or forEach in conjunction with return
Appears that for is faster than some: jsperf.com/array-some-vs-loop
@MudassirAli I hope you can accept this answer no?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.