用于快速查找和有序循环的 JavaScript 数据结构
JavaScript 中是否有一种数据结构或模式可以用于快速查找(通过键,如关联数组)和有序循环?
是的,现在我使用对象文字来存储我的数据,但我刚刚发现 Chrome 在循环属性名称时不保持顺序。
JavaScript 有没有通用的方法来解决这个问题?
Is there a data structure or a pattern in JavaScript that can be used for both fast lookup (by key, as with associative arrays) and for ordered looping?
Right, now I am using object literals to store my data, but I just discovered that Chrome does not maintain the order when looping over the property names.
Is there a common way to solve this in JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
自己创建一个数据结构。将顺序存储在结构内部的数组中。将键映射的对象存储在常规对象中。我们将其称为
OrderedMap
,它将有一个映射、一个数组和四个基本方法。插入元素时,将其添加到数组中所需的位置以及对象中。按索引插入或在末尾插入的时间复杂度为 O(1)。
删除对象时,将其从数组和对象中删除。如果按键或值删除,复杂度为 O(n),因为您需要遍历维持排序的内部数组。按索引删除时,复杂度为 O(1),因为您可以直接访问数组和对象中的值。
查找的时间复杂度为 O(1)。通过键从关联数组(对象)中检索值。
遍历将是有序的并且可以使用任一方法。当需要有序遍历时,创建一个包含对象(仅值)的数组并返回它。作为一个数组,它不支持键控访问。另一种选择是要求客户端提供一个回调函数,该函数应应用于数组中的每个对象。
请参阅 Closure 库中的 Google 实现 LinkedMap,了解相关文档和源代码这样的一堂课。
Create a data structure yourselves. Store the ordering in an array that is internal to the structure. Store the objects mapped by a key in a regular object. Let's call it
OrderedMap
which will have a map, an array, and four basic methods.When inserting an element, add it to the array at the desired position as well as to the object. Insertion by index or at the end is in O(1).
When deleting an object, remove it from the array and the object. If deleting by a key or a value, complexity is O(n) since you will need to traverse the internal array that maintains ordering. When deleting by index, complexity is O(1) since you have direct access to the value in both the array and the object.
Lookups will be in O(1). Retrieve the value by key from the associative array (object).
Traversal will be ordered and can use either of the approaches. When ordered traversal is required, create an array with the objects (values only) and return it. Being an array, it would not support keyed access. The other option is to ask the client to provide a callback function that should be applied to each object in the array.
See Google's implementation of a LinkedMap from the Closure Library for documentation and source for such a class.
Chrome 不维护对象字面量中键的顺序的唯一情况似乎是键是数字。
在 Chrome 中,上面的结果是:“1,9,342,damsonplum,banana,apple,cherry”。
在其他浏览器中,它会生成“damsonplum,9,banana,1,apple,cherry,342”。
因此,除非你的键是数字,否则我认为即使在 Chrome 中,你也是安全的。如果您的键是数字,也许只需在它们前面加上一个字符串即可。
The only instance in which Chrome doesn't maintain the order of keys in an object literal seems to be if the keys are numeric.
In Chrome, the above produces: "1,9,342,damsonplum,banana,apple,cherry".
In other browsers, it produces "damsonplum,9,banana,1,apple,cherry,342".
So unless your keys are numeric, I think even in Chrome, you're safe. And if your keys are numeric, maybe just prepend them with a string.
作为
已注明(如果您的键是数字)
您可以在它们前面添加一个字符串以保持顺序。
示例
As
has been noted, if your keys are numeric
you can prepend them with a string to preserve order.
Example