[英]How come the 'in' keyword isn't able to find an element in my array?
Below is returning the true 下面是返回真
a = 'status'
a=='status'
while below is returning false
而下面的返回
false
a = 'status'
a in ['status'] --> False
If '=='
is returning true
then please let me know why 'in'
is returning false
? 如果
'=='
返回true
那么请让我知道为什么'in'
返回false
?
The in
operator doesn't check if an array/collection contains an element. in
运算符不检查数组/集合是否包含元素。 It checks if the object on the right has a property with the name of the value on the left. 它检查右侧的对象是否具有名称左侧的值的属性。
// Both return 'true'.
'foo' in { foo: 100, bar: 200 }
'filter' in ['hello']
You probably want to use indexOf
: 您可能要使用
indexOf
:
['status'].indexOf(a) >= 0
Or for newer versions of JavaScript, includes
: 或对于较新版本的JavaScript,
includes
:
['status'].includes(a)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.