5

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.

Mark
  • 32,293
  • 33
  • 107
  • 137
  • 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 Nov 28 '10 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 Nov 28 '10 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 Nov 28 '10 at 23:52
  • @Šime Vidas thanks I updated the question @Ignacio Vazquez-Abrams thank you too. – Mark Nov 29 '10 at 00: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 Nov 29 '10 at 00:06
  • @Alex I updated the question to explain what I want to do. Looks like I didn't ask the right question. – Mark Nov 29 '10 at 00:09
  • @Mark I take it you have no control of the JSON output, as it would be best just to send the last three. – Orbling Nov 29 '10 at 00:10
  • @Mark If your keys are numerical, then you should just use arrays and not objects. If you use arrays, you can easily grab the last 3 items by calculating with the `length` property – Šime Vidas Nov 29 '10 at 00:18
  • @Šime Vidas this is json i'm getting from the server. I don't think there's any way to get arrays. @orbling ahh it is my server. i think i'll ask about that in another question. – Mark Nov 29 '10 at 00:20
  • @Mark Sure you can: `{ [ 1, 2, 3 ] }` - you just put the array inside an object. – Šime Vidas Nov 29 '10 at 00:23
  • @Šime Vidas I don't get it, what do I do? please write an answer using my example. – Mark Nov 29 '10 at 00:26
  • @Mark Yea, that would not work, since an object needs a key-value pair. But this works: ...................... `{ "arr": [ 1, 2, 3 ] }`. You just define an object with one key, which value is the array that you need. – Šime Vidas Nov 29 '10 at 00:31
  • @Šime Vidas you're saying I should do this server side before sending? Otherwise i don't understand how that would work. – Mark Nov 29 '10 at 00:35
  • @Mark Yes, I am talking about the JSON data that is retrieved from the server. How are you generating the JSON string on the server? Or is it static? – Šime Vidas Nov 29 '10 at 00:38
  • @Šime Vidas it's from mongodb. i'm grabbing a document and this object it's a document and its subdocuments. – Mark Nov 29 '10 at 01:28

4 Answers4

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.

Adam Lassek
  • 35,156
  • 14
  • 91
  • 107
  • 4
    @Ignacio this isn't a PHP question. – Adam Lassek Nov 28 '10 at 23:48
  • 3
    @Ignacio He was talking about JavaScript objects. – Šime Vidas Nov 28 '10 at 23:49
  • 1
    This is because Javascript "associative arrays" are actually objects with properties, rather than actual arrays. – Orbling Nov 29 '10 at 00:08
  • Is the problem only with IE? Pretty cool. This is very good to know, since I'm not really doing any such moving about with the client. – Mark Nov 29 '10 at 02:12
  • 2
    That quote is no longer quite true. Both Chrome and Opera (from 10.50) order properties whose names are unsigned 32-bit integers before all others (which then appear in insertion order). – gsnedders Nov 29 '10 at 04:16
3

Use this on json objects arrays

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

Hope this helps

Rasheed
  • 991
  • 1
  • 12
  • 18
2

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]
}); 
zoli
  • 469
  • 6
  • 17
1

Follow the json in reverse order,

for(json.length;i>=0;i--)
{
    console.log(json.[i]);
}
Calete
  • 71
  • 1
  • 1
  • 5