最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - reverse json javascript - Stack Overflow

programmeradmin11浏览0评论

Is there an inexpensive way to reverse:

{
    "10": "..."
    "11": "...",
    "12": "...",
    "13": "...",
    "14": "...",
}

so that I get:

{
    "14": "...",
    "13": "...",
    "12": "..."
    "11": "...",
    "10": "...",
}

reverse() doesn't seem to work on json objects. The only way I can think of is to loop through all the elements and create an array. feels like there should be a better way.

Edit: thanks for all the help UPDATE:

What about let's say if each key has chronological data. When I use $.each on the object, it runs through the objects from top to bottom, I didn't realize that was unreliable.

Here's what I'm trying to do:

$.each(object, function (key, value) {
  function foo (key, value);
});

I want to not run foo on all but the last 3 pairs, that is I only want to use the last 3 pairs. I figured if I can reverse them I can just run the first three and stop.

Is there any way I can just do the last 3? If the last 3 ordering is unreliable, is there a safer way to grab the last 3. The last 3 will have the largest numerical keys.

Thanks.

Edit 2: I'm basically deciding finally to do the manipulations on the server side. I'm reorganizing my database so that the relevant subdocuments are now full on documents that could be queried with mongodb. Thanks.

Is there an inexpensive way to reverse:

{
    "10": "..."
    "11": "...",
    "12": "...",
    "13": "...",
    "14": "...",
}

so that I get:

{
    "14": "...",
    "13": "...",
    "12": "..."
    "11": "...",
    "10": "...",
}

reverse() doesn't seem to work on json objects. The only way I can think of is to loop through all the elements and create an array. feels like there should be a better way.

Edit: thanks for all the help UPDATE:

What about let's say if each key has chronological data. When I use $.each on the object, it runs through the objects from top to bottom, I didn't realize that was unreliable.

Here's what I'm trying to do:

$.each(object, function (key, value) {
  function foo (key, value);
});

I want to not run foo on all but the last 3 pairs, that is I only want to use the last 3 pairs. I figured if I can reverse them I can just run the first three and stop.

Is there any way I can just do the last 3? If the last 3 ordering is unreliable, is there a safer way to grab the last 3. The last 3 will have the largest numerical keys.

Thanks.

Edit 2: I'm basically deciding finally to do the manipulations on the server side. I'm reorganizing my database so that the relevant subdocuments are now full on documents that could be queried with mongodb. Thanks.

Share Improve this question edited Nov 29, 2010 at 2:19 Mark asked Nov 28, 2010 at 23:43 MarkMark 33.6k34 gold badges108 silver badges139 bronze badges 15
  • 2 I really have to question what you're trying to accomplish by doing this. Most languages that have a JSON parser will not care what order the pairs are in. – Ignacio Vazquez-Abrams Commented Nov 28, 2010 at 23:46
  • 2 @Mark reverse() is an Array method - and you got objects. Also, You want to create "another array", but what you got are not arrays but plain objects. – Šime Vidas Commented Nov 28, 2010 at 23:46
  • @Mark I think that both code-snippets will create objects with the same characteristics. Once the object literals are evaluated, the information about the order of the properties in the literal is lost. – Šime Vidas Commented Nov 28, 2010 at 23:52
  • @Šime Vidas thanks I updated the question @Ignacio Vazquez-Abrams thank you too. – Mark Commented Nov 29, 2010 at 0:05
  • 1 Order is never guaranteed in JSON objects. I don't understand why you would want this anyway. No matter what order the properties are in, you will still access the elements in the same fashion. By their key name. – Alex Commented Nov 29, 2010 at 0:06
 |  Show 10 more comments

4 Answers 4

Reset to default 8

Javascript associative arrays are unordered. You cannot depend on the properties being in any particular order.

From Mozilla Developer Network:

Although ECMAScript makes iteration order of objects implementation-dependent, it may appear that all major browsers support an iteration order based on the earliest added property coming first (at least for properties not on the prototype). However, in the case of Internet Explorer, when one uses delete on a property, some confusing behavior results, preventing other browsers from using simple objects like object literals as ordered associative arrays. In Explorer, while the property value is indeed set to undefined, if one later adds back a property with the same name, the property will be iterated in its old position--not at the end of the iteration sequence as one might expect after having deleted the property and then added it back.

So if you want to simulate an ordered associative array in a cross-browser environment, you are forced to either use two separate arrays (one for the keys and the other for the values), or build an array of single-property objects, etc.

Use this on json objects arrays

jsonObjectArray.reverse();
$.each(jsonObjectArray, function(i, item) {
    //do something with the item
});

Hope this helps

This might help. Get all the keys from the json objects into an array, which you can sort.

var a = { 1 : 'x', 3 : 'y', 2 : 'z' };
var keys = []
for (i in a) { keys.push(i); }
keys.sort();

then you can use reverse() and slice() to just iterate over the keys you need.

$.each(keys, function(idx, key) { 
  // do whatever with a[key]
}); 

Follow the json in reverse order,

for(json.length;i>=0;i--)
{
    console.log(json.[i]);
}
发布评论

评论列表(0)

  1. 暂无评论