数组的长度和未定义的指标 [英] Array length and undefined indexes

查看:203
本文介绍了数组的长度和未定义的指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想了解如何数组JavaScript的工作,但我在这里有一个复杂的问题。

首先,我建立了我的数组:

  VAR ARR = [];

,并在其中设置一些元素:

 改编[5] =一件事;
ARR [2] =未定义;

我觉得我应该有大小2的数组,因为我只有在2具体指标两个对象。

:所以我使用数组的。长度属性进行了测试

 的document.write(arr.length +< BR>中);

结果,有趣的是,为6。但它必须包含两个项目。其大小如何能6?它可能是与我所用,在这里最新的指数相关的改编[5] =一件事;

然后我试图遍历它:

  VAR大小= 0;
对于(ARR中的变种X){
 大小++;
}

以及尺寸变量现在是2。所以,我从中学到:如果我使用了一个循环,我将计算性能是它,而不是它的最后一个索引。

但是,如果我试图的document.write(ARR [4])(其中尚未设定),把它写入未定义

那么,为什么改编[2] 计算在的for..in 循环,而不是改编[4]

让我回答我的问题:什么我在想的typeof不确定==未定义这是令人惊讶的事实。但是,这是JavaScript中,我们需要用它自己的规则行事:)

的jsfiddle 及以下片段。

\r
\r

VAR ARR = [];\r
\r
ARR [5] =一件事;\r
ARR [2] =未定义;\r
的document.write(arr.length +< BR>中);\r
\r
变种大小= 0;\r
对于(ARR中的变种X){\r
 大小++;\r
}\r
\r
的document.write(尺寸+< BR>中);\r
\r
的document.write(ARR [4] +< BR>中);

\r

\r
\r


解决方案

注意:数组索引都不过是Array对象的属性。

引用MDN的<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Relationship_between_length_and_numerical_properties\">Relationship 长度和数值的属性部分之间,


  

当设置一个JavaScript阵列上的属性,当属性是一个有效的数组索引和索引是数组的当前范围之外,发动机会更新数组的长度相应的属性。


引用ECMA脚本5规格阵列的对象


  

<强>每当一个属性被添加其名称是一个数组索引,长度属性被改变时,如果需要的话,为比数组索引的数值多一个;每当length属性被改变,每个属性的名字是一个数组索引其值不小于新长度将被自动删除。


所以,当你设置为首页 5 A值,JavaScript引擎调整数组的长度 6


引用ECMA脚本5规格阵列的对象


  

属性名称 P (在一个字符串值的形式)是一个数组的索引,当且仅当的ToString(ToUint32(P)) 等于 P ToUint32(P)不等于2 32 -1。


所以,你的情况 2 4 是有效的指标,但只有 2 在数组中所定义。您可以确认这样

  arr.hasOwnProperty(2)

其它索引不在数组中定义呢。所以,你的数组对象被称为的稀疏数组对象


  

那么,为什么ARR [2] for..in循环计数,而不是ARR [4]不计?


的for..in 枚举对象的所有有效的枚举的属性。在你的情况,因为只有 2 在数组中有效的属性,它会被计算在内。

但是,当你打印改编[4] ,它打印未定义,因为JavaScript将返回未定义,如果您尝试访问未在对象定义的属性。例如,

 的console.log({} ['名']);
//未定义

同样,因为 4 未在尚未定义的改编未定义被返回。


虽然我们在这个问题上,你可能需要阅读这些答案,以及,

I just want to understand how JavaScript arrays work but I have a complicated problem here.

First I created my array:

var arr = [];

And set some elements in it:

arr[5] = "a thing";
arr[2] = undefined;

I thought that I should have an array of size 2, because I only have two objects at 2 specific indexes. So I tested it with the .length property of arrays:

document.write(arr.length + "<br>");

The result, interestingly, is 6. But it must contain two items. How can its size be 6? It is probably related with the latest index that I used, here arr[5] = "a thing";

I then tried to loop over it:

var size = 0;
for(var x in arr){
 size++;   
}

And the size variable is now 2. So, what I learned from this: if I use a for in loop, I will calculate how many properties are in it, not its last index.

But if I try to document.write(arr[4]) (which is not set yet), it writes undefined.

So why is arr[2] counted in the for..in loop, but not arr[4]?

Let me answer my question: what I was thinking about typeof undefined == undefined which is amazingly true. But this is JavaScript, we need to play with it using his own rules :)

jsFiddle and snippet below.

var arr = [];

arr[5] = "a thing";
arr[2] = undefined;
document.write(arr.length + "<br>");

var size = 0;
for(var x in arr){
 size++;   
}

document.write(size + "<br>");

document.write(arr[4] + "<br>");

解决方案

Note: Array indexes are nothing but properties of Array objects.

Quoting MDN's Relationship between length and numerical properties section,

When setting a property on a JavaScript array when the property is a valid array index and that index is outside the current bounds of the array, the engine will update the array's length property accordingly.

Quoting ECMA Script 5 Specification of Array Objects,

whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted

So, when you set a value at index 5, JavaScript engine adjusts the length of the Array to 6.


Quoting ECMA Script 5 Specification of Array Objects,

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, in your case 2 and 4 are valid indexes but only 2 is defined in the array. You can confirm that like this

arr.hasOwnProperty(2)

The other indexes are not defined in the array yet. So, your array object is called a sparse array object.

So why arr[2] is counted in for..in loop and not arr[4] is not counted?

The for..in enumerates all the valid enumerable properties of the object. In your case, since only 2 is a valid property in the array, it will be counted.

But, when you print arr[4], it prints undefined, because JavaScript will return undefined, if you try to access a property which is not defined in an object. For example,

console.log({}['name']);
// undefined

Similarly, since 4 is not yet defined in the arr, undefined is returned.


While we are on this subject, you might want to read these answers as well,

这篇关于数组的长度和未定义的指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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