JavaScript数组随机索引的插入和删除
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaScript数组随机索引的插入和删除,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3638字,纯文字阅读大概需要6分钟。
内容图文
我正在使用随机创建的索引将一些项插入到数组中,例如:
var myArray = new Array();
myArray[123] = "foo";
myArray[456] = "bar";
myArray[789] = "baz";
...
换句话说,数组索引不以零开头,它们之间会有“数字间隙”.我的问题是:
>这些数字间隙是否会以某种方式分配(因此需要一些内存),即使它们没有指定值?
>当我从上面的示例中删除myArray [456]时,此项目下方的项目是否会被重新定位?
编辑:
关于我在插入/删除后重新定位项目的问题/关注 – 我想知道内存会发生什么,而不是索引.来自wikipedia article的更多信息:
Linked lists have several advantages
over dynamic arrays. Insertion of an
element at a specific point of a list
is a constant-time operation, whereas
insertion in a dynamic array at random
locations will require moving half of
the elements on average, and all the
elements in the worst case. While one
can “delete” an element from an array
in constant time by somehow marking
its slot as “vacant”, this causes
fragmentation that impedes the
performance of iteration.
解决方法:
Will these numeric gaps be somehow allocated (and therefore take some memory) even when they do not have assigned values?
不,JavaScript数组根本不是真正的数组(见下文),未使用的索引不占用内存.
When I delete myArray[456] from upper example, would items below this item be relocated?
如果你在谈论数组索引,它取决于你如何删除它:如果你使用delete关键字,没有.如果使用拼接功能或类似功能,是的.就内存而言,不,其他条目不会被重新定位(无论如何),并且由不再存在的条目引用的任何内存(无论是因为删除还是拼接或弹出或类似)都可以通过以下方式回收:垃圾收集器.链接列表在JavaScript中几乎没有优于数组或普通旧对象的优势,您很少看到它们.添加到JavaScript数组(或对象)可能是一个接近恒定时间的操作(实现可能需要进行散列,可能需要对B树结构或类似的进行遍历,但这完全是特定于实现的),因为删除.
正如您所描述的那样,正如Zevon指出的那样,您可能根本不需要阵列.如果需要一个length属性或一个依赖于它的数组函数,你真的只需要一个数组.否则,你最好用一个普通的旧物体:
var obj = {};
obj[123] = "foo";
obj[456] = "bar";
obj[789] = "baz";
这是完全有效的JavaScript.您在括号(123等)中使用的值被强制转换为字符串(无论您是处理数组还是普通对象),因此密钥实际上是“123”等等(无论您是使用数组或对象).您甚至可以使用for..in控制结构(details here)循环遍历它们.
我的意思是“……根本不是阵列”?从字面上看. JavaScript对象是键 – >值映射,JavaScript数组只不过是具有键和值的对象,以及对作为数字字符串的键的特殊处理,以及特殊长度属性.虽然我们通常将数组“索引”作为数字写入,但所有属性名称都是字符串 – [0]转换为[“0”](尽管实现可以自由优化,只要行为保持符合规范).这包含在the specification的第15.4节中,该段以本段开头:
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 2^32?1. A property whose property name is an array index is also called an element. Every Array object has a
length
property whose value is always a nonnegative integer less than 2^32. The value of thelength
property is numerically greater than the name of every property whose name is an array index; whenever a
property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever a property is added whose name is an array index, thelength
property is changed, if necessary, to be one more than the numeric value of that array index; and whenever thelength
property is changed, every property whose name is an array index whose value is not smaller than the new
length is automatically deleted. This constraint applies only to own properties of an Array object and is unaffected by length or array index properties that may be inherited from its prototypes.
内容总结
以上是互联网集市为您收集整理的JavaScript数组随机索引的插入和删除全部内容,希望文章能够帮你解决JavaScript数组随机索引的插入和删除所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。