数组的长度和未定义的指标 [英] Array length and undefined indexes
问题描述
我只是想了解如何数组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 及以下片段。
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
注意:数组索引都不过是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 afor 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 writesundefined
.So why is
arr[2]
counted in thefor..in
loop, but notarr[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 to6
.
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 ifToString(ToUint32(P))
is equal toP
andToUint32(P)
is not equal to 232−1.So, in your case
2
and4
are valid indexes but only2
is defined in the array. You can confirm that like thisarr.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 only2
is a valid property in the array, it will be counted.But, when you print
arr[4]
, it printsundefined
, because JavaScript will returnundefined
, 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 thearr
,undefined
is returned.
While we are on this subject, you might want to read these answers as well,
这篇关于数组的长度和未定义的指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!