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...)?

2

9 Answers 9

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]);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I'll try to make additional array with numeric keys.
12

There is another solution, a fairly easy one:

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

That's it.

Comments

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.

1 Comment

empty object > reverse is not a function
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);
});

Comments

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!
});

Comments

0

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

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

1 Comment

I wish I could, but it works for arrays only not for objects.
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'];
    });
}

Comments

0

jsonObj = [];

            $.each(data.reverse(), function (i, dt) {
                jsonObj.push({
                    'id': dt.id
                });

Comments

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]; });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.