In Lodash, on the other hand, returning false tells _.forEach() that this iteration will be the last. Is there a way to make the “continue” behavior also functional in Lodash?
You could return true
, or just a single return
(which returns undefined
), this value is different from needed false
for “exit iteration early by explicitly returning false
.”
_.forEach([1, 2, 3, 4, 5], function (a) {
if (a < 3) return; // continue
console.log(a);
if (a > 3) return false; // break
// return undefined; // continue, undefined is the standard value of ending a function
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>