3

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;
    }
}
6
  • 3
    Are you using a browser that supports array.indexOf. Commented Feb 5, 2013 at 12:14
  • 1
    IE <=8, for example, doesn't support indexOf() for Arrays. Commented Feb 5, 2013 at 12:23
  • I've tried IE9, Firefox 15 and Chrome 24. Are there actually browsers that wouldn't support indexOf()? Bit silly if you ask me... Commented Feb 5, 2013 at 12:24
  • make the scope of issArray global and do not pass it to your checkIss() function and see whether it works Commented Feb 5, 2013 at 12:27
  • Thanks for your comments, but it appears it's the software that I'm coding for that doesn't support indexOf. I find it a bit odd that such a simple function isn't supported but heyho, I'll raise a bug and see what happens! Commented Feb 5, 2013 at 12:59

1 Answer 1

2

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

1 Comment

Thanks ElPedro, it looks like I'll ne using this!

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.