简体   繁体   中英

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

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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