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;
}
}
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;
}
}
Share
Improve this question
edited Feb 5, 2013 at 12:16
Cerbrus
73k19 gold badges136 silver badges150 bronze badges
asked Feb 5, 2013 at 12:13
user2042970user2042970
331 gold badge1 silver badge3 bronze badges
6
- 3 Are you using a browser that supports array.indexOf. – adeneo Commented Feb 5, 2013 at 12:14
-
1
IE <=8, for example, doesn't support
indexOf()
for Arrays. – Tim Down 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... – user2042970 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 – asifsid88 Commented Feb 5, 2013 at 12:27
- Thanks for your ments, 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 Commented Feb 5, 2013 at 12:59
1 Answer
Reset to default 2Easiest to just loop through the array and pare 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;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745453443a4628375.html
评论列表(0条)