forEach loop out the js

Reason : Recent data processing method using _.foreach lodash.js in the project, need not be performed later js code ends the loop condition is satisfied.

    Foreach does not break because the default method. Return false used in the attempt; finding is not successful.

to sum up:

Error Method: return false; // will still complete the implementation cycle, but no longer behind the implementation cycle js code

 

_.forEach (tmArray, function (value, index, Collection) { 
   the let TEMP = tmArray [index]; 
   _.forEach (tmArray, function (V2, index2, coll2) {
      IF (index2 =! index) {
      IF (_. intersection (TEMP, tmArray [index2]). length> 0 ) { 
     top.Dialog.alert ( "first" + (index + 1) + " program and the second article" + (index2 + 1) + " program cycle overlap Article Please reset "! );
      return  false ; 
   } 
  } 
 }) 
});

 

The reason: foreach can not be terminated before the end of the cycle, return false just ended this cycle.

 

The right way: foreach could not be terminated because through the normal process, the method can throw an exception, forced termination.

. 1  the try {
 2                          _.forEach (tmArray, function (value, index, Collection) {
 . 3                              the let TEMP = tmArray [index];
 . 4                              _.forEach (tmArray, function (V2, index2, coll2) {
 . 5                                  IF ! (Index2 = index) {
 . 6                                      IF (_.intersection (TEMP, tmArray [index2]). length> 0 ) {
 . 7                                          top.Dialog.alert ( "first" + (index + 1) + " program and the second article" + (index2 + 1) + "Article planning cycle overlap, please re-set!" );
 8                                          the throw  new new Error ( "breakForEach");
 9                                         return false;
10                                     }
11                                 }
12                             })
13                         });
14                     } catch (e) {
15                         if (e.message!="breakForEach") throw e;
16                     }

 

Guess you like

Origin www.cnblogs.com/salmonLeeson/p/11321093.html