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

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

问题描述

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

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

但是,我发现浏览器不在通过它们时保证一个特定的对象顺序,所以在上面的3将出现在7之前。我切换到使用这样的数组格式:

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

现在,我可以循环正确的顺序,但不能快速查找,例如 data [3] 而不必循环通过数组。



有没有一个很好的方式来结合两者方法?我宁愿避免为每个格式使用单独的对象,因为对象是相当大的(数百个元素)。

解决方案

我也遇到这个问题。一个解决方案是除了原始对象之外还要保留有序的键数组。

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

现在,如果您想要第二个元素,您可以执行此查找:

  var second_object = objects [order [1]]; 

ECMA标准没有说明任何对象中元素的顺序。具体来说,当Chrome键入时,Chrome会重新排列键。
示例:

  var example = {
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);
},function(key1,key2){
return object [key1] - object [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天全站免登陆