I have the following js function and I'm pulling my hair out trying to determine why it won't return 'true'. I've verified the logic block is being hit by adding an alert into the block but it seems to skip the line containing the return statement.
function requiresMatchLevel(fields) {
$.each(fields, function (i, field) {
if (field.OperationParamName() == "MatchLevel" && field.Include()) {
return true;
}
});
return false;
};
return
acts on a per-function basis. Returning in the function passed to $.each
will not have your outer function return something.
If you return something in the function passed to $.each
, jQuery will receive the result. The only value that has effect here is false
, which makes jQuery break out of the loop.
function requiresMatchLevel(fields) {
var result = false;
$.each(fields, function (i, field) {
if (field.OperationParamName() == "MatchLevel" && field.Include()) {
result = true;
return false; // break out of loop - no need to continue
}
});
return result;
};
How about
function requiresMatchLevel(fields) {
var isMatch = false;
$.each(fields, function (i, field) {
if (field.OperationParamName() == "MatchLevel" && field.Include()) {
isMatch = true;
}
});
return isMatch;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With