为什么在测试不包含 0 的数组中是否存在 0 时,javascript 的“in”运算符返回 true?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3067072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:10:25  来源:igfitidea点击:

Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?

javascriptarraysin-operator

提问by Mariano Peterson

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"?

为什么 Javascript 中的“in”运算符在测试数组中是否存在“0”时返回 true,即使数组似乎不包含“0”?

For example, this returns true, and makes sense:

例如,这返回 true,并且有意义:

var x = [1,2];
1 in x; // true

This returns false, and makes sense:

这将返回 false,并且是有道理的:

var x = [1,2];
3 in x; // false

However this returns true, and I don't understand why:

然而,这返回true,我不明白为什么:

var x = [1,2];
0 in x;

回答by Matthew Flaschen

It refers to the index or key, not the value. 0and 1are the valid indices for that array. There are also valid keys, including "length"and "toSource". Try 2 in x. That will be false (since JavaScript arrays are 0-indexed).

它指的是索引或键,而不是值。 01是该数组的有效索引。还有有效的键,包括"length""toSource"。试试2 in x。那将是错误的(因为 JavaScript 数组是 0 索引的)。

See the MDN documentation.

请参阅MDN 文档

回答by Dean Harding

The inoperator doesn't do what you're thinking it does. The inoperator returns trueif the specified operand is a property of the object. For arrays, it returns trueif 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, ...)

in运营商不这样做,你在想它做什么。该in运营商的回报true,如果指定的操作数是对象的属性。对于数组,true如果操作数是有效索引,则返回(如果将数组视为特殊情况对象,其中属性仅命名为 0、1、2...,则这是有意义的)

For example, try this:

例如,试试这个:

javascript:var x=[1,4,6]; alert(2 in x);

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 returns true.

它也会返回true,因为“2”是数组的有效索引。同样,“0”是数组的索引,因此也返回true.

回答by Sean

Javascript's inoperator does not check if a value is contained in an array. It checks if the object has a property or index. So var x = [4,5]; 4 in x; //false 1 in x; //true.

Javascript 的in运算符不检查值是否包含在数组中。它检查对象是否具有属性或索引。所以var x = [4,5]; 4 in x; //false 1 in x; //true

Because length is a property of x, "length" in x; //true

因为长度是 x 的一个属性, "length" in x; //true

回答by kennebec

Modern browsers, except IE, support a couple methods that can find a value in an array.

现代浏览器(IE 除外)支持几种可以在数组中查找值的方法。

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.

indexOf 和 lastIndexOf 返回数组中其参数完全匹配的第一个(或最后一个)索引,如果未找到匹配元素,则返回 -1。

if(A.indexOf(0)!= -1){
    // the array contains an element with the value 0.
}

You can add one or both methods to IE and older browsers-

您可以向 IE 和旧浏览器添加一种或两种方法-

if(![].indexOf){
    Array.prototype.indexOf= function(what, i){
        i= i || 0;
        var L= this.length;
        while(i< L){
            if(this[i]=== what) return i;
            ++i;
        }
        return -1;
    }
    Array.prototype.lastIndexOf= function(what, i){
        var L= this.length;
        i= i || L-1;
        if(isNaN(i) || i>= L) i= L-1;
        else if(i< 0) i += L;
        while(i> -1){
            if(this[i]=== what) return i;
            --i;
        }
        return -1;
    }
}

回答by Brady Huang

I guess you use Python before, in JS, use Array.prototype.includes

我猜你之前用过 Python,在 JS 中,使用 Array.prototype.includes

let x = [1, 2]
x.includes(1) // true

inoperator check the indices of the array not the value

运算符中检查数组的索引而不是值

0 in [1, 2] // true
2 in [1, 2] // false

回答by LeOn - Han Li

just like the js for inloop which iterates the object properties, the inoperator checks if the specified property is in the specified object or its prototypechain. it does not check whether an elementis in an array.

就像for in迭代对象属性的 js循环一样,in操作符检查指定的属性是否在指定的对象或其原型链中。它不检查元素是否在数组中。

let's say x is an array, use x.includes(element)to return true/falsein nodejs/modern-browsers. As for loop, use for(let element of x)since x is a js iterable.

假设 x 是一个数组,用于在 nodejs/现代浏览器中x.includes(element)返回true/false。至于loop,使用for(let element of x)因为 x 是一个 js iterable