简体   繁体   English

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

[英]Why does javascript's “in” operator return true when testing if 0 exists in an array that doesn't contain 0?

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;

It refers to the index or key, not the value.它指的是索引或键,而不是值。 0 and 1 are the valid indices for that array. 01是该数组的有效索引。 There are also valid keys, including "length" and "toString" .还有一些有效的键,包括"length""toString" Try 2 in x . 2 in x尝试2 in x That will be false (since JavaScript arrays are 0-indexed).那将是错误的(因为 JavaScript 数组是 0 索引的)。

See the MDN documentation .请参阅MDN 文档

The in operator doesn't do what you're thinking it does. in运算符不会做您认为它会做的事情。 The in operator returns true if the specified operand is a property of the object.如果指定的操作数是对象的属性,则in运算符返回true For arrays, it returns true 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, ...)对于数组,如果操作数是有效索引,则返回true (如果将数组视为特殊情况对象,其中属性仅命名为 0、1、2...,则这是有意义的)

For example, try this:例如,试试这个:

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

It'll also return true , because "2" is a valid index into the array.它也会返回true ,因为“2”是数组的有效索引。 In the same way, "0" is an index into the array, so also returns true .同样,“0”是数组的索引,因此也返回true

Javascript's in operator does not check if a value is contained in an array. Javascript 的in运算符不检查值是否包含在数组中。 It checks if the object has a property or index.它检查对象是否具有属性或索引。 So var x = [4,5]; 4 in x; //false 1 in x; //true所以var x = [4,5]; 4 in x; //false 1 in x; //true var x = [4,5]; 4 in x; //false 1 in x; //true var x = [4,5]; 4 in x; //false 1 in x; //true . var x = [4,5]; 4 in x; //false 1 in x; //true

Because length is a property of x, "length" in x; //true因为长度是 x 的属性,所以 x 中的"length" in x; //true "length" in x; //true

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;
    }
}

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

in operator check the indices of the array not the value运算符中检查数组的索引而不是值

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

just like the js for in loop which iterates the object properties, the in operator checks if the specified property is in the specified object or its prototype chain.就像 js for in循环迭代对象属性一样, in运算符检查指定的属性是否在指定的对象或其原型链中。 it does not check whether an element is in an array.它不检查元素是否在数组中。

let's say x is an array , use x.includes(element) to return true/false in nodejs/modern-browsers.假设 x 是一个数组,使用x.includes(element)在 nodejs/modern-browsers 中返回true/false As for loop , use for(let element of x) since x is a js iterable .至于loop ,使用for(let element of x)因为 x 是一个 js iterable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (JavaScript,Redux)为什么我的嵌套数组在返回时被删除,但是当我不返回时却没有? - (JavaScript, Redux) Why are my nested array's getting deleted when returning, however when I don't return it doesn't? 当没有方法的返回值时,为什么 javaScript 的可选链接语法不适用于空值合并运算符? - Why javaScript's Optional Chaining Syntax does not work with Nullish Coalescing Operator when there is no method's return value? 如果id不存在,为什么$('#id')返回true? - Why does $('#id') return true if id doesn't exist? 为什么我的2D数组不返回true - Why doesn't my 2D array return true 为什么模运算符返回true? - Why does the modulo operator return true? 为什么in运算符对于未定义的属性返回true? - Why does the in operator return true for a property that is undefined? 为什么Javascript的OR返回的值不是true / false? - Why does Javascript's OR return a value other than true/false? 为什么Javascript的instanceof对于这些本机构造函数返回true? - Why does Javascript's instanceof return true for these native constructors? JavaScript if语句何时应返回true - JavaScript if statement doesn't return true when it should 为什么JavaScript的“ in”运算符会错误地返回false? - Why does JavaScript's “in” operator return false incorrectly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM