user2042970
user2042970

Reputation: 33

Javascript array indexOf returns undefined

When calling my function checkIss(), issFullArray.indexOf(issToCheck) always returns undefined. I've run a .length, output the contents of issFullArray, I can't figure out why it's not working- the array looks fine to me. As you can see below, I've tried explicitly setting issArray as an array and copying the array returned by my getIssList()

function updateIss() {
    var issArray = [];

    var currService = current.u_business_service;
    var currIss = current.u_is_service;
    issArray = getIssList(currService).slice(); //getIssList() returns an arry
    if (checkIss(issArray, currIss) === false) {
        //do stuff
    }
}

function checkIss(issFullArray, issToCheck) {
    if (issFullArray.indexOf(issToCheck) < 0) {
        return false;
    } else {
        return true;
    }
}

Upvotes: 3

Views: 10850

Answers (1)

ElPedro
ElPedro

Reputation: 586

Easiest to just loop through the array and compare each value and return true if there is a match otherwise return false. Not much more code and works for all browsers.

function checkIss(issFullArray, issToCheck) {
    for(i=0; i<issFullArray.length; i++) {
        if(issFullArray[i]==issToCheck) {
            return true;
        }
    }
    return false;
}

Upvotes: 2

Related Questions