gpt4 book ai didi

javascript - 使用indexOf查找数组中的项目

转载 作者:行者123 更新时间:2023-12-03 10:05:06 24 4
gpt4 key购买 nike

我对“indexOf”有疑问。当我在参数中传递数值时,它可以工作,但不能与变量一起使用:

$.ajax({
url: 'bdd.php',
type: 'POST',
data: {'jsonNomCommune': jsonNomCommune, 'client': client},
success: function(data) {
data = JSON.parse(data);
listClients= data.clients;
listProspects = data.prospects;

$('#tableCSV tbody tr ').each(function(){
var id = $(this).attr('id');
id=id.substring(4);

if (listClients.indexOf(id) > -1){
$(this).css("background-color","red");

}

if (listProspects.indexOf(id) > -1){
$(this).css("background-color","blue");
}


});
}
});

listProspects 和 listClients 是数值数组。例如,27 在 listClients 中,因此当 id=27 时,“listClients.indexOf(id) > -1”应该有效,但事实并非如此。当我写:“(listClients.indexOf(27) > -1)”时,它起作用了。

问题出在哪里?

最佳答案

27"27" 不同。

尝试将 id 解析为整数:

listClients.indexOf(+id) //note, its parsing

或者:

listClients.indexOf(parseInt(id, 10))

关于javascript - 使用indexOf查找数组中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30394106/

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