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 ?
mapmethod, which is intended to, hmmm, map values, so it's not supposed to stop the iteration[].every(returntrueto continue, non-trueto break) or[].some- return true to break, non-true to continue.[].reduce. Passing[]as a second argument, and only modifying when conditions are met.forEachwhich has a much different solution.