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;
    }
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
user2042970
  • 33
  • 1
  • 1
  • 3
  • 3
    Are you using a browser that supports array.indexOf. – adeneo Feb 05 '13 at 12:14
  • 1
    IE <=8, for example, doesn't support `indexOf()` for Arrays. – Tim Down Feb 05 '13 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... – user2042970 Feb 05 '13 at 12:24
  • make the scope of issArray global and do not pass it to your checkIss() function and see whether it works – asifsid88 Feb 05 '13 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! – user2042970 Feb 05 '13 at 12:59
  • IE8 do not support array.indexOf. so try this: http://stackoverflow.com/questions/1744310/how-to-fix-array-indexof-in-javascript-for-internet-explorer-browsers/29003069#29003069 – Will V King Mar 12 '15 at 06:59

1 Answers1

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;
}
ElPedro
  • 576
  • 10
  • 17