48

I can't break _.forEach loop, help me to do that.

_.forEach(oIncludedFileMap, function (aIncludedFiles, sKey) {
  if(aIncludedFiles == null){
     break;
  }
});
alexmac
  • 19,087
  • 7
  • 58
  • 69
Mangesh Darekar
  • 505
  • 1
  • 5
  • 5

1 Answers1

97

To finish lodash#forEach method use return false; statement:

_.forEach(oIncludedFileMap, function(aIncludedFiles, sKey) {
  if  (aIncludedFiles == null) {
    return false;
  }
});
alexmac
  • 19,087
  • 7
  • 58
  • 69
  • 1
    is `return false` a compulsion? OR will a normal `return` also work? – parwatcodes Sep 13 '17 at 16:40
  • 12
    @p0k8_ `return` without `false` just terminates the current iteration. – alexmac Sep 13 '17 at 16:46
  • 4
    https://lodash.com/docs/4.17.4#forEach Quote: Iteratee functions may exit iteration early by explicitly returning false. – skirtle Sep 13 '17 at 17:04
  • 14
    @p0k8_ If you just write `return` that will implicitly return `undefined`, exactly the same as if the function didn't return at all. There would be no way for Lodash to tell the difference based on the value it receives. `return false` is equivalent to `break` whereas `return` is equivalent to `continue`. – skirtle Sep 13 '17 at 17:07