Friday, February 21, 2014

a return inside a forEach loop in JavaScript seems to sabotage the forEach loop

call: function(app, key) {
   if(app._appSettings === undefined) {
      return null;
   } else {
      app._appSettings.forEach(function(setting) {
         if (setting.ahCode == key) {
            if (setting.ahValue) {
               return setting.ahValue;
            } else {
               return setting.ahBooleanValue;
            }
         }
      });
   }
}

 
 

What is above ALWAYS returned null no matter the circumstance because a return inside a forEach loop in JavaScript seems to sabotage the forEach loop. I had to change it to:

call: function(app, key) {
   if(app._appSettings === undefined) {
      return null;
   } else {
      var returnMe = "null";
      app._appSettings.forEach(function(setting) {
         if (setting.ahCode == key) {
            if (setting.ahValue) {
               returnMe = setting.ahValue;
            } else {
               if (setting.ahBooleanValue == null) {
                  
//return null
               } else {
                  if (setting.ahBooleanValue) {
                     returnMe = "true";
                  } else {
                     returnMe = "false";
                  }
               }
            }
         }
      });
      if (returnMe == "null") {
         return null;
      }
      if (returnMe == "true") {
         return true;
      }
      if (returnMe == "false") {
         return false;
      }
      return returnMe;
   }
}

No comments:

Post a Comment