Sladex
Sladex

Reputation: 303

Reverse object in jQuery.each

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

Upvotes: 13

Views: 30255

Answers (9)

Kareem
Kareem

Reputation: 5404

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

Upvotes: 3

Robert McMahan
Robert McMahan

Reputation: 551

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

Upvotes: 0

Araya Yamir
Araya Yamir

Reputation: 89

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.

Upvotes: 8

Muhammad Bilal
Muhammad Bilal

Reputation: 389

jsonObj = [];

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

Upvotes: 0

BentFX
BentFX

Reputation: 2836

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

Upvotes: 0

xorinzor
xorinzor

Reputation: 6467

There is another solution, a fairly easy one:

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

That's it.

Upvotes: 12

Alex K.
Alex K.

Reputation: 51

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

Upvotes: 5

user113716
user113716

Reputation: 322492

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

Upvotes: 15

Ivan
Ivan

Reputation: 714

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

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

Upvotes: 0

Related Questions