按原始顺序遍历jQuery JSON对象

时间:2021-01-14 13:38:47

I've got some json data in a map object which is sorted by time. The key is an integer id and the value is an object which contains a timestamp. However when I try to iterate over this data with the jQuery $.each function, the results come back sorted by the key instead. How can I iterate over my collection of objects in their original order?

映射对象中有一些json数据按时间排序。键是一个整数id,值是一个包含时间戳的对象。但是,当我尝试使用jQuery $迭代此数据时。每个函数,返回的结果按键排序。如何以原始顺序遍历对象集合?

Code example:

代码示例:

$.getJSON(url, addPages);
function addPages(pageData) {
    $.each(pageData, function(key,value){
        alert(key+' : '+value);
    }
}

4 个解决方案

#1


6  

You could copy the key value pairs into an array of objects and then use Array.sort to put them in order.

您可以将键值对复制到一个对象数组中,然后使用数组。按顺序排列。

// $.getJSON(url, addPages); //removed for test purposes
function addPages(pageData) {
    var pageItems = [];
    $.each(pageData, function(key,value){
        pageItems.push( { key: key, value: value } );
    });
    // Assuming you can do arithmetic on timestamps
    pageItems.sort( function(a,b){ return a.value.timeStamp - b.value.timeStamp; } ); 
    $.each(pageItems, function(index,pageItem){
        alert(pageItem.key+' : '+pageItem.value.timeStamp);
    });
}

My test script:

我的测试脚本:

var testData = { 
  '12': { timeStamp: 55 },
  '16': { timeStamp: 655 },
  '123': { timeStamp: 455 },
  '312': { timeStamp: 955 },
  '132': { timeStamp: 255 },
  '126': { timeStamp: 455 },
  '162': { timeStamp: 355 }
  };
addPages(testData);

#2


11  

Objects are un-ordered sets. You can't specify an order on them in a cross-browser complaint manner. There is no concept of original order unless it's already ordered by a rule.

对象是un-ordered集。您不能以跨浏览器的抱怨方式为它们指定订单。没有原始顺序的概念,除非它已经由规则排序。

So sort the data on the server and then enumerate over it in the same sorted order.

因此,对服务器上的数据进行排序,然后以相同的排序顺序对其进行枚举。

Alternatively format the JSON to be

或者将JSON格式化为

{ "values": [
   { "someKey": "someVal" },
   { "someOtherKey": "someOtherVal" }
] }

You can iterate over the array with a for (i = 0; i < len; i++) loop and "garantuee" the original order.

可以用for (i = 0)迭代数组;我 <兰;i++)循环和“garantuee”原始顺序。< p>

#3


2  

In firefox objects keep the order.. in chrome they don't. No idea about other browser but two different implementations already show that you cannot rely on it.

在firefox中,对象保持顺序。在chrome他们不。除了两种不同的实现之外,对其他浏览器一无所知,这表明您不能依赖它。

If you need something ordered, use a list; if you need something with both non-numeric keys and a certain order, the easiest way would be using a list of [key, value] lists:

如果你需要订购的东西,请使用列表;如果你需要非数字键和一定顺序的东西,最简单的方法是使用[key, value]列表:

[['abd', 'def'], ['ghi', 'jkl']]

#4


0  

If by "original" you mean the timestamp in the object, I don't think there's any foolproof option other than to first explicitly sort the array, and only then loop through it.

如果“原始”指的是对象中的时间戳,我认为除了先显式地对数组进行排序,然后再对其进行循环之外,没有任何简单的选项。

#1


6  

You could copy the key value pairs into an array of objects and then use Array.sort to put them in order.

您可以将键值对复制到一个对象数组中,然后使用数组。按顺序排列。

// $.getJSON(url, addPages); //removed for test purposes
function addPages(pageData) {
    var pageItems = [];
    $.each(pageData, function(key,value){
        pageItems.push( { key: key, value: value } );
    });
    // Assuming you can do arithmetic on timestamps
    pageItems.sort( function(a,b){ return a.value.timeStamp - b.value.timeStamp; } ); 
    $.each(pageItems, function(index,pageItem){
        alert(pageItem.key+' : '+pageItem.value.timeStamp);
    });
}

My test script:

我的测试脚本:

var testData = { 
  '12': { timeStamp: 55 },
  '16': { timeStamp: 655 },
  '123': { timeStamp: 455 },
  '312': { timeStamp: 955 },
  '132': { timeStamp: 255 },
  '126': { timeStamp: 455 },
  '162': { timeStamp: 355 }
  };
addPages(testData);

#2


11  

Objects are un-ordered sets. You can't specify an order on them in a cross-browser complaint manner. There is no concept of original order unless it's already ordered by a rule.

对象是un-ordered集。您不能以跨浏览器的抱怨方式为它们指定订单。没有原始顺序的概念,除非它已经由规则排序。

So sort the data on the server and then enumerate over it in the same sorted order.

因此,对服务器上的数据进行排序,然后以相同的排序顺序对其进行枚举。

Alternatively format the JSON to be

或者将JSON格式化为

{ "values": [
   { "someKey": "someVal" },
   { "someOtherKey": "someOtherVal" }
] }

You can iterate over the array with a for (i = 0; i < len; i++) loop and "garantuee" the original order.

可以用for (i = 0)迭代数组;我 <兰;i++)循环和“garantuee”原始顺序。< p>

#3


2  

In firefox objects keep the order.. in chrome they don't. No idea about other browser but two different implementations already show that you cannot rely on it.

在firefox中,对象保持顺序。在chrome他们不。除了两种不同的实现之外,对其他浏览器一无所知,这表明您不能依赖它。

If you need something ordered, use a list; if you need something with both non-numeric keys and a certain order, the easiest way would be using a list of [key, value] lists:

如果你需要订购的东西,请使用列表;如果你需要非数字键和一定顺序的东西,最简单的方法是使用[key, value]列表:

[['abd', 'def'], ['ghi', 'jkl']]

#4


0  

If by "original" you mean the timestamp in the object, I don't think there's any foolproof option other than to first explicitly sort the array, and only then loop through it.

如果“原始”指的是对象中的时间戳,我认为除了先显式地对数组进行排序,然后再对其进行循环之外,没有任何简单的选项。