从嵌套循环java脚本返回值(Return the value from nested loop java script)
我写了一个涉及多个循环的函数。
$scope.matchFunction = function() { angular.forEach(datas, function(data) { angular.forEach(data.innerdatas, function(nnerdata) { if (innerdata.id === 'ABCD') { console.log('matched'); //matched return true and break and stop executon return true; } }); }); return false; };但我总是最终回归虚假。
我想我无法从嵌套循环返回。
任何帮助。
I have written a function which is involving multiple loops.
$scope.matchFunction = function() { angular.forEach(datas, function(data) { angular.forEach(data.innerdatas, function(nnerdata) { if (innerdata.id === 'ABCD') { console.log('matched'); //matched return true and break and stop executon return true; } }); }); return false; };But I am always ending up in returning false.
I think I am not able to return from nested loops.
Any help.
最满意答案
您没有从main函数返回,只返回内部函数的值。
您可以使用变量来存储状态,然后可以返回它
$scope.matchFunction = function () { var valid = false; angular.forEach(datas, function (data) { angular.forEach(data.innerdatas, function (nnerdata) { if (innerdata.id === 'ABCD') { console.log('matched'); //matched return true and break and stop executon valid = true; } }); }); return valid; };You are not returning from the main function, you are returning the value only from the inner function.
You can use a variable to store the state and then can return it like
$scope.matchFunction = function () { var valid = false; angular.forEach(datas, function (data) { angular.forEach(data.innerdatas, function (nnerdata) { if (innerdata.id === 'ABCD') { console.log('matched'); //matched return true and break and stop executon valid = true; } }); }); return valid; };更多推荐
发布评论