gpt4 book ai didi

javascript - 在 JavaScript 中搜索命名数组

转载 作者:行者123 更新时间:2023-12-02 18:40:08 25 4
gpt4 key购买 nike

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

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

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

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

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

如何进行此类搜索?

最佳答案

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

最佳实践detect properties在一个对象中。在你的情况下,也许你需要转换为字符串

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

关于javascript - 在 JavaScript 中搜索命名数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16920193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com