208

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 ?

Community
  • 1
  • 1
Mudassir Ali
  • 7,913
  • 4
  • 32
  • 60
  • 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 – Yoni Sep 04 '12 at 09:36
  • 24
    @Yoni is correct. If the OP is looking for a breakable forEach loop, consider uing methods such as [`[].every`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/every) (return `true` to continue, non-`true` to break) or [`[].some`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/some) - return true to break, non-true to continue. – Rob W Sep 04 '12 at 09:40
  • alternatively [`[].reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce). Passing `[]` as a second argument, and only modifying when conditions are met. – Sandy Gifford Aug 01 '17 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. – Sandy Gifford Aug 01 '17 at 16:06
  • 1
    @SandyGifford Agreed. Voting to reopen this question. – AndrewL64 Jan 30 '19 at 18:14
  • Since this question is still closed, here's an alternate solution: https://codepen.io/SandoCalrissian/pen/YBNPpX – Sandy Gifford Jan 30 '19 at 22:32
  • https://stackoverflow.com/a/64384888/1929115 – Shankar Ganesh Jayaraman Oct 16 '20 at 10:12

1 Answers1

272

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;
});
João Silva
  • 89,303
  • 29
  • 152
  • 158
  • 7
    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! – TheOneWhoPrograms Dec 17 '15 at 16:34
  • 6
    Why isn't it possible with the `Array.prototype.map` ? – Vadorequest Dec 13 '16 at 16:13
  • 10
    @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` – Mudassir Ali Mar 12 '17 at 12:59
  • 2
    Appears that `for` is faster than `some`: https://jsperf.com/array-some-vs-loop – cellepo May 25 '18 at 21:57
  • @MudassirAli I hope you can accept this answer no? – Sampath Jun 01 '20 at 13:35