So I'm using a map function to iterate and array, and I want to stop going trough elements inside a map function once a condition has been met.
Is there a way to do so ?
Ex :
var arr = [1,2,3,4,5,6,7,8,9,10];
arr.map((el,index)=>{
console.log("Element : ",el)
if(el > 5){
//Stop the map function and proceed to the next operation
//like goto line 11 so skip going trough the map anymore
console.log("Stop")
}
})
console.log("Sucess!")
So in this example I just want to stop the map once the element inside it, has a value bigger than 5. Is there a way to do this with map, or I should find another solution ?
Why map ? -Seems like an intresting solution, and I was wondering if it's possible to use in y case
You are not looking for .map function, but you need to use .some or .find
var arr = [1,2,3,4,5,6,7,8,9,10];
arr.some((el,index)=>{
console.log("Element : ",el)
if(el > 5){
//Stop the map function and proceed to the next operation
//like goto:11
console.log("Stop")
return true // stop the iteration of the first "truty" value
}
return false
})
console.log("Sucess!")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With