Find value in array then return true with Angular forEach

34,951

Solution 1

The first option is different because return true; returns from the function which is passed as parameter to forEach function not from the outer function isInArrayNgForeach and that's why the last line return false; gets called always when the forEach finishes. Which makes the function return false always.

If you change the code like this, tt will return expected result:

function isInArrayNgForeach(field, arr) {
    var result = false;

    angular.forEach(arr, function(value, key) {
        if(field == value)
            result = true;
    });

    return result;
}

Solution 2

function isInArrayNgForeach(field, arr) {
    angular.forEach(arr, function(value, key) {
        if(field == value)
            return true; // You are returning the immediate function here not isInArrayNgForeach
   ...

To make it work as you intend it to

function isInArrayNgForeach(field, arr) {
    var result = false;
    angular.forEach(arr, function(value, key) {
        if(field == value)
            result = true;
    });
    return result;
} // This returns expected value

Solution 3

In function isInArrayNgForeach you are returning true to the anonymous function given to forEach. It'll never go to isInArrayNgForeach. And at last you are saying return false which will be always returned.

It is like

function isInArrayNgForeach(field, arr) {
    //do something except return
    return false;
} // So it will always return false

If you want to achieve what you are doing in your second function, the javascript native some will help you.

function isInArrayNgForeach(field, arr) {
    return arr.some(function(element){return element==field});
    //it will check if any element is equal to field it will return true otherwise false
}
Share:
34,951
Kimchi Man
Author by

Kimchi Man

Updated on August 11, 2022

Comments

  • Kimchi Man
    Kimchi Man over 1 year

    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.

  • Kimchi Man
    Kimchi Man almost 10 years
    Thanks for (the detail explanation && the example code)
  • Prashant Pokhriyal
    Prashant Pokhriyal almost 6 years
    after setting result=true, you should return, otherwise it'll iterate for every element even after a match.