Reverse json javascript

Is there an inexpensive way to flip:

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

To obtain:

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

reverse () does not seem to work on json objects. The only way I can think of is to iterate over all the elements and create an array. feels like there should be a better way.

Edit: thanks for the help. UPDATE:

What can I say if each key has historical data. When I use $ .each on an object, it goes through the objects from top to bottom, I did not realize that this is unreliable.

Here is what I am trying to do:

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

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

, 3? 3 , 3. 3 .

.

2: , , . , , mongodb. .

+5
4

Javascript . , - .

Mozilla:

ECMAScript , , ( ). Internet Explorer, , , , . , undefined, , - , .

, - , ( , ) ..

+8

json

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

,

+3

This can help. Get all the keys to json objects into an array that 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 the reverse () and slice () functions to just iterate over the keys you need.

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

Follow json in reverse order

for(json.length;i>=0;i--)
{
    console.log(json.[i]);
}
+1
source

All Articles