40
rated 0 times
[
41]
[
1]
/ answers: 1 / hits: 37505
/ 10 Years ago, thu, july 31, 2014, 12:00:00
I'm learning JavaScript and AngularJS.
What's the difference between this code?
function isInArrayNgForeach(field, arr) {
angular.forEach(arr, function(value, key) {
if(field == value)
return true;
});
return false;
} // This returns always false
function isInArrayJavaScript(field, arr) {
for(var i = 0; i < arr.length; i++) {
if(field == arr[i])
return true;
}
return false;
} // This works fine
function isInArray() {
var testArr = ['stack', 'over', 'flow'];
console.log(isInArrayNgForeach('stack', testArr)); // return false
console.log(isInArrayJavaScript('stack', testArr)); // return true
}
My question is: why isInArrayNgForeach always return false? I assume that because there is a function inside of the function, but I'm not sure why.
More From » angularjs