简体   繁体   English

在javascript中搜索命名数组

[英]Search a named array in javascript

This might be a duplicate but I could not help but post this, I have an array with this format 这可能是一个重复,但我忍不住发布这个,我有一个这种格式的数组

answerCollection= {
0:
{selected : a,
 status :  false   
 },
1:
{selected : a,
 status :  false   
 }
}

I want to do an index check on the this aray like this 我想像这样对这个aray做一个索引检查

if(answerCollection.indexOf(indexNo) == -1){
 sel = "a";
 }

but it keeps failing ie I keep getting a return value of -1, irrespectively of if the index exist in the array or not. 但它仍然失败,即我继续得到-1的返回值,而不管索引是否存在于数组中。

How do I do this kind of search? 我该怎么做这种搜索?

if(indexNo in answerCollection){
 sel = "a";
 }

Best practice to detect properties in an object. 检测对象中属性的最佳实践。 In your case, maybe you need to convert to string 在你的情况下,也许你需要转换为字符串

if(indexNo.toString() in answerCollection){
     sel = "a";
     }
if(answerCollection[indexNo] == null){
 sel = "a";
}

for example look here . 比如看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM