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

发布于 2024-09-05 19:21:30 字数 311 浏览 4 评论 0原文

为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

远山浅 2024-09-12 19:21:30

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

请参阅 MDN 文档

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

See the MDN documentation.

生生漫 2024-09-12 19:21:30

in 运算符不会执行您想象的操作。如果指定的操作数是对象的属性,则 in 运算符返回 true。对于数组,如果操作数是有效的索引,则返回 true(如果将数组视为属性简单命名为 0、1 的特殊情况对象,则这是有意义的) , 2, ...)

例如,试试这个:

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

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

The in operator doesn't do what you're thinking it does. The in operator returns true if the specified operand is a property of the object. 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, ...)

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. In the same way, "0" is an index into the array, so also returns true.

盛夏已如深秋| 2024-09-12 19:21:30

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

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

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

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

I guess you use Python before, in JS, use 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
凑诗 2024-09-12 19:21:30

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. So var x = [4,5]; 4 in x; //false 1 in x; //true.

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

当爱已成负担 2024-09-12 19:21:30

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

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

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

您可以向 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;
    }
}

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.

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-

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;
    }
}
南汐寒笙箫 2024-09-12 19:21:30

就像 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, the in 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 return true/false in nodejs/modern-browsers. As for loop, use for(let element of x) since x is a js iterable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文