为什么删除项目时Javascript中的数组大小不调整? [英] Why don't arrays in Javascript resize when you remove an item?
问题描述
在许多语言中,删除项目后,标准动态列表(非固定大小数组)类型将重新调整大小:
In many languages, the standard dynamic list (not fixed-size array) type will resize after an item is deleted:
Python:
myList = ['a', 'b', 'c']
del(myList[0])
print(len(myList)) # Prints '2'
C# :
var myList = new List<string> {"a", "b", "c"};
myList.RemoveAt(0);
Console.WriteLine(myList.Count); // Prints '2'
,依此类推。
但是,即使元素的值为 undefined
(这告诉我,它与 array [index] = undefined
):
However, in Javascript, the list length stays the same, even though the element evaluates to undefined
(this tells me that it's different to array[index] = undefined
):
JavaScript:
var myArray = ['a', 'b', 'c']
delete myArray[0]
console.log(myArray.length) // Prints '3'
console.log(myArray) // Prints "[ , 'b', 'c' ]" in node, '[undefined × 1, "b", "c"]' in Chrome
myArray[0] = undefined
console.log(myArray) // Prints "[ undefined, 'b', 'c' ]" on both node and Chrome
myArray.splice(0, 1)
console.log(myArray) // Prints "['b', 'c']"
我的问题是:
- 是JS数组
Delete
- 替换被删除的数组元素的未定义样值到底是什么?
- 究竟是什么? >
- Is the JS array
delete
behaviour by design or an oversight that couldn't be fixed for fear of breaking legacy code? - What exactly is the 'undefined-like' value that replaces the deleted array element?
推荐答案
您将删除
与<$的比较c $ c> RemoveAt ,这是不正确的。 删除
不执行其操作;为此,您可以使用 splice
:
Your comparison of delete
to RemoveAt
and such is incorrect. delete
doesn't do what they do; to do that, you'd use splice
:
var myArray = ['a', 'b', 'c']
myArray.splice(0, 1);
console.log(myArray.length) // Prints '2'
删除
会删除条目而无需重新排序其他条目,创建一个 sparse 数组(一个数组,其中某些条目完全丢失)。 删除
并不是真正用于数组,而是用于从对象中删除属性。但它适用于数组,因为JavaScript 根本不是真正的数组 *,它们是对某些属性进行特殊处理的对象,例如名称为数组索引(即定义为字符串名称 ...其数值 i
的范围是 +0≤i< 2 ^ 32-1
)和 length
。
delete
removes the entry without reordering other entries, creating a sparse array (an array with some entries missing entirely). delete
isn't really meant for arrays, it's meant for removing properties from objects; but it applies to arrays because standard (e.g., non-typed) arrays in JavaScript aren't really arrays at all*, they're objects with special handling for certain properties, such as those whose names are "array indexes" (which are defined as string names "...whose numeric value i
is in the range +0 ≤ i < 2^32-1
") and length
.
长度
的值,如果您发生以下情况:
length
's value is changed if you:
- 使用任何方法添加或删除条目(
拼接
,推
,等等。) - 通过分配等于或大于
length
的索引来添加条目。 - 直接分配给
length
:如果增加length
,则使数组稀疏;如果减少它,则删除该索引或更高索引处的所有条目。例如,如果您有一个数组['a','b','c','d','e']
并执行theArray .length = 3
,它将不会在索引3('d'
)或4('e
- Use any method to add or remove entries (
splice
,push
, etc.). - Add an entry by assigning to an index equal to or greater than
length
. - Assign directly to
length
: If you increaselength
, you make the array sparse; if you decrease it, you remove any entries at that index or above. E.g., if you have an array['a', 'b', 'c', 'd', 'e']
and dotheArray.length = 3
, it won't have entries at indexes 3 ('d'
) or 4 ('e'
) anymore.
JS数组是设计删除行为还是
Is the JS array delete behaviour by design or an oversight that couldn't be fixed for fear of breaking legacy code?
通过设计,是否有可能因疏忽而破坏了遗留代码?如上所述,删除
主要是关于从对象中删除属性。
By design. As mentioned above, delete
is primarily about removing properties from objects.
确切是什么是未定义的值,它将替换已删除的数组元素
What exactly is the 'undefined-like' value that replaces the deleted array element
没有一个。这是标准数组是对象这一事实的另一个副产品:如果您尝试从一个不存在的对象中读取属性的值,则获取操作的结果是值未定义
。这并不是说对象中有些东西具有 undefined
之类的值,而是该对象中根本没有具有该名称的属性。此插图可能有用:
There isn't one. This is another by-product of the fact standard arrays are objects: If you try to read the value of a property from an object that it doesn't have, the result of that "get" operation is the value undefined
. It's not that there's something in the object with an undefined
-like value, it's that there's no property in that object with that name at all. This illustration may be useful:
var o = {}; // A non-array object
console.log(o.noSuchProperty); // undefined
console.log(o["noSuchProperty"]); // undefined
console.log(o[2]); // undefined
var a = []; // An array object
console.log(a.noSuchProperty); // undefined
console.log(a["noSuchProperty"]); // undefined
console.log(a[2]); // undefined
* (这是我贫乏的小博客上的帖子)
这篇关于为什么删除项目时Javascript中的数组大小不调整?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!