javascript - Break _.each loop -
how break _.each() loop? tried _.every(), looping through data once.
sample code:
_.each([1,2,3,4,5],function(num){ if(num < 3) console.log(num) else{ console.log(num); return false; } });
output: 1 2 3 4 5
_.every([1,2,3,4,5],function(num){ if(num < 3) console.log(num) else{ console.log(num); return false; } });
output: 1 false
you cannot escape functional foreach loop.
consider using regular loop instead.
const arr = [1,2,3,4,5]; (let = 0; < arr.length; ++i) { const num = arr[i]; if(num < 3) { console.log(num) } else { console.log('break out'); break; } }
Comments
Post a Comment