简体   繁体   English

为什么javascript为具有object属性而不是indexed元素的数组显示0长度

[英]Why javascript shows 0 length for array with object property instead of indexed element

For Example: 例如:

var arr = [];
arr[3.4] = 1;
console.log(arr.length);

In the above code sample the length property holds zero why and what happened inside JS parser because it's length is zero. 在上面的代码示例中, length属性为零,为什么以及JS解析器内部发生了什么,因为它的长度为零。

An array's length is reflective of the largest array index present in the array. 数组的length反映了数组中存在的最大数组索引 An "array index" (see below) is a property name that is an integer value less than 2 32 −1. “数组索引”(见下文)是一个属性名称,它是一个小于2 32 -1的整数值。 Since 3.4 is not an integer, setting it does not alter the length . 由于3.4不是整数,因此设置它不会改变length

Arrays are objects, so there is no reason why an array can't have a property named 3.4 , but that property doesn't influence the length because its name does not fit the criteria of an array index. 数组是对象,因此没有理由为什么数组不能具有名为3.4的属性,但该属性不会影响length因为它的名称不符合数组索引的条件。

ES2015 9.4.2, Array Exotic Objects defines an "array index" as an integer less than 2 32 −1: ES2015 9.4.2,Array Exotic Objects将“数组索引”定义为小于2 32 -1的整数:

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 2 32 −1." 当且仅当ToString(ToUint32( P ))等于P并且ToUint32( P )不等于2 32 -1时,属性名P (以String值的形式)是数组索引

And that definition is used in relation to the length value (emphasis mine): 该定义用于length值(强调我的):

Every Array object has a length property whose value is always a nonnegative integer less than 2 32 . 每个Array对象都有一个length属性,其值始终是小于2 32的非负整数。 The value of the length property is numerically greater than the name of every own property whose name is an array index ; length属性的值在数值上大于名称为数组索引的每个属性的名称 ; whenever an own property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. 无论何时创建或更改Array对象的自有属性,都会根据需要调整其他属性以保持此不变量。 Specifically, whenever an own property is added whose name is an array index, the value of the length property is changed , if necessary, to be one more than the numeric value of that array index... 具体来说, 每当添加名称为数组索引的属性时 ,如果需要, length属性的值将更改为该数组索引的数值之一...

A JavaScript array can not have fractional indexes. JavaScript数组不能包含小数索引。

What you have done there is assigned a property called "3.4" on the array object. 你在那里做了什么在数组对象上分配了一个名为"3.4"的属性。

This does not affect the length property, which is designed to return one number higher than the highest valid index. 这不会影响length属性,该属性旨在返回一个高于最高有效索引的数字。

If you think about how arrays are supposed to work, you should realise a fractional offset doesn't make sense. 如果你考虑数组应该如何工作,你应该意识到分数偏移是没有意义的。

You could try using an object vs an array. 您可以尝试使用对象与数组。

var arr = {};
arr[3.4] = 1;
arr[3.5] = 2;
arr[3.6] = 3;
console.log(Object.keys(arr).length);

您必须使用整数作为索引,而不是浮点数。

var arr = []; arr[3] = 1; console.log(arr.length);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM