This question already has answers here:
Why does javascript's “in” operator return true when testing if 0 exists in an array that doesn't contain 0?
                            
                                (6个答案)
                            
                    
                4年前关闭。
        

    

无法弄清楚为什么总是返回false:

var filter_clients = [25,351];
var data = 351;
console.log(filter_clients);
console.log(data);
if(data in filter_clients) console.log('in');
else console.log('out');


fiddle

最佳答案

使用数组时,in运算符适用于索引而不适用于值。例子:

0 in filter_clients //true
1 in filter_clients //true
2 in filter_clients //false


这是一种实现您要执行的操作的方法

filter_clients.indexOf(data) != -1

关于javascript - in运算符中总是返回false ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31015624/

10-09 17:21