为什么 javascript 的“in”是测试不包含 0 的数组中是否存在 0 时,运算符返回 true?
为什么 JavaScript 中的“in”运算符在测试数组中是否存在“0”时返回 true,即使数组似乎不包含“0”?
例如,这返回 true,并且有意义:
var x = [1,2];
1 in x; // true
这返回 false,并且有意义:
var x = [1,2];
3 in x; // false
但是,这返回 true,我不明白为什么:
var x = [1,2];
0 in x;
Why does the "in" operator in Javascript return true when testing if "0" exists in array, even when the array doesn't appear to contain "0"?
For example, this returns true, and makes sense:
var x = [1,2];
1 in x; // true
This returns false, and makes sense:
var x = [1,2];
3 in x; // false
However this returns true, and I don't understand why:
var x = [1,2];
0 in x;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它指的是索引或键,而不是值。
0
和1
是该数组的有效索引。还有有效的键,包括"length"
和"toString"
。尝试2 in x
。这将是错误的(因为 JavaScript 数组是从 0 索引的)。请参阅 MDN 文档。
It refers to the index or key, not the value.
0
and1
are the valid indices for that array. There are also valid keys, including"length"
and"toString"
. Try2 in x
. That will be false (since JavaScript arrays are 0-indexed).See the MDN documentation.
in
运算符不会执行您想象的操作。如果指定的操作数是对象的属性,则in
运算符返回true
。对于数组,如果操作数是有效的索引,则返回 true(如果将数组视为属性简单命名为 0、1 的特殊情况对象,则这是有意义的) , 2, ...)例如,试试这个:
它也会返回
true
,因为“2”是数组的有效索引。同样,“0”是数组的索引,因此也返回true
。The
in
operator doesn't do what you're thinking it does. Thein
operator returnstrue
if the specified operand is a property of the object. For arrays, it returnstrue
if the operand is a valid index (which makes sense if think of arrays as a special-case object where the properties are simply named 0, 1, 2, ...)For example, try this:
It'll also return
true
, because "2" is a valid index into the array. In the same way, "0" is an index into the array, so also returnstrue
.我猜你之前使用过Python,在JS中,使用Array.prototype.includes
in 运算符检查数组的索引而不是值
I guess you use Python before, in JS, use Array.prototype.includes
in operator check the indices of the array not the value
Javascript 的
in
运算符不会检查数组中是否包含值。它检查对象是否具有属性或索引。所以 var x = [4,5]; 4 x ; //x 中为假 1; //真。因为长度是 x 的属性,所以
x 中的“length”; //真
Javascript's
in
operator does not check if a value is contained in an array. It checks if the object has a property or index. Sovar x = [4,5]; 4 in x; //false 1 in x; //true
.Because length is a property of x,
"length" in x; //true
现代浏览器(IE 除外)支持几种可以查找值的方法
在一个数组中。
indexOf 和 lastIndexOf 返回精确匹配的第一个(或最后一个)索引
其参数在数组中的大小,如果未找到匹配元素,则返回 -1。
您可以向 IE 和旧版浏览器添加一种或两种方法 -
Modern browsers, except IE, support a couple methods that can find a value
in an array.
indexOf and lastIndexOf return the first(or last) index of an exact match
of their argument in an array, or -1, if no matching element was found.
You can add one or both methods to IE and older browsers-
就像 js
for in
循环迭代对象属性一样,in
运算符检查指定属性是否在指定对象或其原型中链。它不会检查元素是否在数组中。假设x是一个数组,在nodejs/modern-browsers中使用
x.includes(element)
返回true/false
。至于循环
,请使用for(let element of x)
,因为x是一个js可迭代
。just like the js
for in
loop which iterates the object properties, thein
operator checks if the specified property is in the specified object or its prototype chain. it does not check whether an element is in an array.let's say x is an array, use
x.includes(element)
to returntrue/false
in nodejs/modern-browsers. As forloop
, usefor(let element of x)
since x is a jsiterable
.