13

HTML:

<input id="sdata" type="hidden" value='{"1651":["12","1"],"1650":["30","0"],"1649":["20","0"],"1648":["13","2"],"1647":["11","0"],"1646":["10","0"],"1645":["12","0"],"1644":["8","0"],"1643":["16","1"],"1642":["10","1"],"1641":["10","0"],"1640":["18","3"]}' />

JS:

var data = $.parseJSON($('#sdata').val());
$.each(data, function(id, sc) {
    alert(id);
}

OUT: 1640, 1641, 1642, ..., 1651

How to make it in reverse order (ex. 1651, 1650...)?

karim79
  • 339,989
  • 67
  • 413
  • 406
Sladex
  • 193
  • 1
  • 1
  • 12

9 Answers9

15

As it is, you can't in any reliable manner. Because you are enumerating an Object, there is never a guaranteed order.

If you want a guaranteed numeric order, you would need to use an Array, and iterate backwards.


EDIT: This will convert your Object to an Array, and do a reverse iteration.

Note that it will only work if all the properties are numeric.

var data = $.parseJSON($('#sdata').val());
var arr = [];

for( var name in data ) {
    arr[name] = data[name];
}
var len = arr.length;
while( len-- ) {
    if( arr[len] !== undefined ) {
        console.log(len,arr[len]);
    }
}
user113716
  • 318,772
  • 63
  • 451
  • 440
12

There is another solution, a fairly easy one:

$(yourobject).toArray().reverse();

That's it.

alex
  • 479,566
  • 201
  • 878
  • 984
xorinzor
  • 6,140
  • 10
  • 40
  • 70
8

I tried this and it worked perfectly for me.

var data = $.parseJSON($('#sdata').val());
$.each(data.reverse(), function(id, sc) {
    alert(id);
});

The only change is the "reverse()" in line 2.

Manish Jangir
  • 5,329
  • 4
  • 42
  • 75
Araya Yamir
  • 89
  • 1
  • 1
5

If all you need to do is generate some HTML out of your JSON and put generated elements into a container in reverse order you can use jQuery's prependTo method when building your HTML.

var container = $('<div />');
$.each(data, function (key, value) {
    $('<div>' + value + '</div>').prependTo(container);
});
Alex K.
  • 51
  • 1
  • 1
3

For Objects

If you are dealing with an object, reverse() won't work! Instead you can do this to maintain the order.

$.each(Object.keys(myObj).reverse(),function(i,key){
  var value = myObj[key];
  //you have got your key and value in a loop that respects the order, go rock!
});
Kareem
  • 5,068
  • 44
  • 38
0

jsonObj = [];

            $.each(data.reverse(), function (i, dt) {
                jsonObj.push({
                    'id': dt.id
                });
Muhammad Bilal
  • 359
  • 3
  • 5
0

Here's an option that seemed to have worked for me. Hope this helps someone. Obviously only works on simple objects (non-nested) but I suppose you could figure out way to make something more complicated with a little extra work.

var reversed_object = {};
Object.keys(original_object).reverse().forEach(function(key)
{ reversed_object[key] = original_object[key]; });
Robert McMahan
  • 531
  • 5
  • 6
0

You can use javascript function sort() or reverse()

var data = $.parseJSON($('#sdata').val());
data.reverse();
$.each(data, function(id, sc) {
alert(id);
}
Ivan
  • 714
  • 1
  • 12
  • 28
0

I don't know, but for the simple stuff I work with, this function does the job. It doesn't rely on numeric keys. And will flip simple objects top to bottom. I don't understand complex Objects, so I don't know how "robust" it is.

function flipObject(obj){
    var arr = [];
    $.each(obj, function(key, val){
        var temp = new Object;
        temp['key'] = key;
        temp['val'] = val;
        arr.push(temp);
        delete temp;
        delete obj[key];
    });
    arr.reverse();
    $.each(arr, function(key, val){
        obj[val['key']] = val['val'];
    });
}
alex
  • 479,566
  • 201
  • 878
  • 984
BentFX
  • 2,746
  • 5
  • 25
  • 30