0

这可能是重复的,但我忍不住发布了这个,我有一个这种格式的数组

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

我想像这样对这个数组进行索引检查

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

但它一直失败,即我一直得到-1的返回值,无论索引是否存在于数组中。

我该如何进行这种搜索?

4

2 回答 2

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

检测对象属性的最佳实践。在您的情况下,也许您需要转换为字符串

if(indexNo.toString() in answerCollection){
     sel = "a";
     }
于 2013-06-04T14:21:13.863 回答
0
if(answerCollection[indexNo] == null){
 sel = "a";
}

例如看这里

于 2013-06-04T14:20:46.460 回答