下面是返回true
a = 'status'
a=='status'
下面返回false
a = 'status'
a in ['status'] --> False
如果 '=='
返回 true
那么请告诉我为什么 'in'
返回 false
>?
最佳答案
The in
operator不检查数组/集合是否包含元素。它检查右侧的对象是否具有左侧值名称的属性。
// Both return 'true'.
'foo' in { foo: 100, bar: 200 }
'filter' in ['hello']
您可能想使用 indexOf
:
['status'].indexOf(a) >= 0
或者对于较新版本的 JavaScript,includes
:
['status'].includes(a)
关于javascript - 为什么 'in' 关键字无法在我的数组中找到元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48255890/