如何在保持键查找的同时保持 Javascript 对象/数组的有序? [英] How to keep an Javascript object/array ordered while also maintaining key lookups?

查看:14
本文介绍了如何在保持键查找的同时保持 Javascript 对象/数组的有序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些最初存储在通用 Javascript 对象中的数据,以 ID 作为键:

<代码>{"7": {"id":"7","name":"你好"},"3": {"id":"3","name":"World"},...}

然而,我发现浏览器在循环遍历它们时并不能保证特定的对象顺序,因此在上面的3"中会出现在7"之前.我改用这样的数组格式:

<预><代码>[{"id":"7","name":"你好"},{"id":"3","name":"World"},...]

现在,我可以按正确的顺序循环,但不能进行快速查找,例如data["3"] 无需遍历数组.

有没有结合这两种方法的好方法?我宁愿避免为每种格式使用单独的对象,因为该对象非常大(数百个元素).

解决方案

我也遇到过这个问题.一个解决方案是在原始对象之外保留一个有序的键数组.

var 对象 = {"7": {"id":"7","name":"你好"},"3": {"id":"3","name":"World"},...}var order = [ "3", "7", ... ];

现在如果你想要第二个元素,你可以做这个查找:

var second_object = objects[order[1]];

ECMA 标准没有说明对象中元素的顺序.特别是当它们看起来像数字时,Chrome 会对它们重新排序.示例:

var 示例 = {"a": "a","b": "b","1": "1",2":2"};

如果你在 Chrome 中打印这个会得到类似的结果:

<代码>{1:1",2:2","a": "a","b": "b"};

有点酸……但是生活.

您也可以使用 Andy 链接的解决方案,基本上将这两者包装在一个对象中.

我经常使用的另一种方法是自定义地图函数,它允许您指定遍历对象的顺序.通常,您将在向用户打印数据时进行排序,因此在循环和创建表行(例如)时,迭代器将按照排序函数指定的顺序传递行.我认为这是个好主意:)

签名如下:

function map(object, callback, sort_function);

示例用法:

map(object, function (row) {table.add_row(row.header, row.value);}, 函数 (key1, key2) {返回对象[key1] - 对象[key2];});

I have some data which I originally stored in a generic Javascript object, with the ID as a key:

{
  "7": {"id":"7","name":"Hello"},
  "3": {"id":"3","name":"World"},
  ...
}

However, I discovered that browsers do not guarantee a particular object order when looping through them, so in the above "3" would come before "7". I switched to using an array format like this:

[
  {"id":"7","name":"Hello"},
  {"id":"3","name":"World"},
  ...
]

Now, I can loop in the correct order but cannot do fast lookups, e.g. data["3"] without having to loop through the array.

Is there a good way to combine both approaches? I would rather avoid using a separate object for each format, because the object is pretty large (hundreds of elements).

解决方案

I have run across this problem as well. A solution is to keep an ordered array of keys in addition to the original object.

var objects = {
  "7": {"id":"7","name":"Hello"},
  "3": {"id":"3","name":"World"},
  ...
}
var order = [ "3", "7", ... ];

Now if you want the second element you can do this lookup:

var second_object = objects[order[1]];

The ECMA standard does not say anything about the order of the elements in an object. And specifically Chrome reorders the keys when they look like numbers. Example:

var example = {
    "a": "a",
    "b": "b",
    "1": "1",
    "2": "2"
};

if you print this in Chrome will get something like:

{
    1: "1",
    2: "2",
    "a": "a",
    "b": "b"
};

It's a little sour .. but life.

You could use the solution Andy linked as well, basically wrapping these two together in one object.

An alternative that I use a lot is a custom map function that allows you to specify the order in which the object is traversed. Typically you will do sorting when you're printing your data to the user so while you loop and create your table rows (for instance) your iterator will pass the rows in the order your sort function specifies. I thought it was a nice idea :)

The signature looks like:

function map(object, callback, sort_function);

Example usage:

map(object, function (row) {
   table.add_row(row.header, row.value);
}, function (key1, key2) {
   return object[key1] - object[key2];
});

这篇关于如何在保持键查找的同时保持 Javascript 对象/数组的有序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆