javascript 像数据结构一样的Javascript列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3399263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:03:43  来源:igfitidea点击:

Javascript list like data structure?

javascriptarrays

提问by Manux

It seems, if I'm not wrong, that because of the way Javascript handles Objects it is unpractical or inefficient to implement linked lists.

如果我没记错的话,似乎由于 Javascript 处理对象的方式,实现链表是不切实际或低效的。

I would need a data structure in which I could easily do 2 operations(apart from indexing), appending at the end and removing (popping) an object at a given index.

我需要一个数据结构,在其中我可以轻松地执行 2 个操作(除了索引),在末尾追加并删除(弹出)给定索引处的对象。

Is using an Arrayand "recreating" it for each remove operation the optimal solution? I would think not.

使用 anArray并为每个删除操作“重新创建”它是最佳解决方案吗?我想不会。

Any ideas?

有任何想法吗?

采纳答案by Justin

It sounds like the JS Arrayis exactly what you're looking for.
You should be able to use the push and pop functions for the stack-like data structure and splice for the rest of it.

听起来JS Array正是您要找的。
您应该能够对类似堆栈的数据结构使用 push 和 pop 函数,并对其其余部分使用 splice。

回答by DixonD

Actually Arraysupports pushand popoperations: JavaScript Array Object

实际Array支持pushpop操作:JavaScript Array Object

回答by Justin Niessner

You don't have to recreate the Javascript array for each removal. Javascript Arrays have push()and pop()methods to add and remove elements:

您不必为每次删除重新创建 Javascript 数组。JavaScript数组必须push()pop()方法来添加和删除元素:

JavaScript Array Object

JavaScript 数组对象