19

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.

4 Answers 4

39

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for (the detail explanation && the example code)
after setting result=true, you should return, otherwise it'll iterate for every element even after a match.
5
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

Comments

1

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
}

Comments

0

For primitives you could still just use array.indexOf...

var arr = ['foo', 'bar', 0, 22];
if (arr.indexOf('bar') > -1) {
    // 'bar' is in arr 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.