数组中“未定义"元素的 JavaScript 'in' 运算符 [英] JavaScript 'in' operator for `undefined` elements in Arrays

查看:34
本文介绍了数组中“未定义"元素的 JavaScript 'in' 运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码片段:

> a = [1, undefined, undefined, undefined, 3]
  [1, undefined, undefined, undefined, 3]
> b = [1,,,,3]
  [1, undefined × 3, 3]
> 1 in a
  true
> 1 in b
  false

我错过了什么吗?似乎是,根据我在数组中定义 undefined 元素的方式,in 运算符的行为有所不同.

Am I missing something? It seems to be that, depending on how I define undefined elements in an array, the in operator behaves differently.

推荐答案

数组只不过是普通的 JavaScript 对象,具有一些特殊性.引用自 ECMA 5.1 规范的数组对象部分

Arrays are nothing but normal JavaScript objects, with some specialization. Quoting from Array Objects section of ECMA 5.1 Specification

数组对象对特定类别的属性名称给予特殊处理.属性名称 P(以字符串值的形式)是数组索引当且仅当 ToString(ToUint32(P)) 等于 P 且 ToUint32(P) 不等于 232-1.

Array objects give special treatment to a certain class of property names. A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 232−1.

所以,数组索引只不过是数组对象的属性.现在,让我们看看数组中的缺失元素.

So, array indices are nothing but properties of array objects. Now, lets see about missing elements in an Array.

引自 ECMA 5.1 标准规范

数组元素可以在开头、中间或结尾省略元素列表.每当元素列表中的逗号前面没有一个 AssignmentExpression(即开头或之后的逗号另一个逗号),缺少的数组元素有助于Array 并增加后续元素的索引.省略阵列元素未定义.如果元素在结尾处被省略数组,该元素不会影响数组的长度.

Array elements may be elided at the beginning, middle or end of the element list. Whenever a comma in the element list is not preceded by an AssignmentExpression (i.e., a comma at the beginning or after another comma), the missing array element contributes to the length of the Array and increases the index of subsequent elements. Elided array elements are not defined. If an element is elided at the end of an array, that element does not contribute to the length of the Array.

所以,当你说

b = [1,,,,3];

除了元素 1 和 3 之外,所有其他元素都被视为数组中的缺失元素.现在元素 13 对应于属性 04(数组索引以 0 在 JavaScript 中).

except the elements 1 and 3, all others are treated as missing elements in the array. Now elements 1 and 3 correspond to the properties 0 and 4 (Array indices start with 0 in JavaScript).

你可以这样检查

console.log(Object.getOwnPropertyNames(b));
# [ '0', '4', 'length' ]
console.log(Object.getOwnPropertyNames(a));
# [ '0', '1', '2', '3', '4', 'length' ]

只有索引04b中.您可能想知道为什么 a 具有从 04 的属性,尽管这些值是 undefined.因为,索引 1、2 和 3 处的元素被定义为 undefined,在 b 中,我们不知道这些值是什么.这就是为什么它们没有分配给属性(索引).

Only the indices 0 and 4 are in b. You might be wondering why a has the properties from 0 to 4, though the values are undefined. Because, the elements at indices 1, 2 and 3 are defined to be undefined, where as in b, we don't know what those values are. That is why they are not allocated to a property (index).

现在,您正在使用 in 运算符检查 1 是否在 b 中.引自 in 运营商 MDN Doc,

Now, you are checking if 1 is in b, with in operator. Quoting from in operator MDN Doc,

如果指定的属性在指定的对象中,则 in 运算符返回 true.

The in operator returns true if the specified property is in the specified object.

因此,您基本上是在检查 1 是否是 b 的属性之一,而事实并非如此.这就是为什么 b 中的 '1' 返回 false 的原因.

So, you are basically checking if 1 is one of the properties of b, which is not. That is why '1' in b returns false.

如果你想知道 1 是否在数组中,你应该使用 Array.prototype.indexOf,像这样

If you wanted to know if 1 is in the Array or not, you should use Array.prototype.indexOf, like this

console.log(a.indexOf(1));
# 0
console.log(b.indexOf(1));
# 0
console.log(b.indexOf(5));
# -1

Array.prototype.indexOf 如果要搜索的元素不在 Array 中,则返回 -1.所以,你可能想做这样的事情

Array.prototype.indexOf returns -1 if the element being searched is not there in the Array. So, you might want to do something like this

console.log(a.indexOf(1) !== -1);
# true
console.log(b.indexOf(1) !== -1);
# true
console.log(a.indexOf(5) !== -1);
# false

这篇关于数组中“未定义"元素的 JavaScript 'in' 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆