admin管理员组文章数量:1425820
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;
}
本文标签: Javascript array indexOf returns undefinedStack Overflow
版权声明:本文标题:Javascript array indexOf returns undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745453442a2658991.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论