Reverse object in jQuery.each

There is another solution, a fairly easy one:

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

That's it.


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