Mahesh
Mahesh

Reputation: 1081

Iterate through array of json objects

I have array of json object as following

{
    "01/15/2015_1": [
        [
            {
                "sequenceType": -1,
                "delflag": -1
            },
            {
                "sequenceType": -1,
                "delflag": -1
            }
        ]
    ],
    "01/15/2015_2": [
        [
            {
                "sequenceType": -1,
                "delflag": -1
            },
            {
                "sequenceType": -1,
                "delflag": -1
            }
        ]
    ]
}

By iterating it with jquery's each() I am getting object as following order:

1."01/15/2015_1"
2."01/15/2015_2"

But I want it in reverse() say as following:

1."01/15/2015_2"
2."01/15/2015_1"

Need help..

Is it possible with ng-repeat?

Upvotes: 1

Views: 286

Answers (2)

mpneuried
mpneuried

Reputation: 181

An Objekt has no defined order, so you cannot make sure this works well in every browser.

You have to grab the keys, Object.keys(), sort them and loop through it by key.

e.g.:

var obj = {
    "01/15/2015_1": [
       "example1"
    ],
    "01/15/2015_3": [
        "example3"
    ],
    "01/15/2015_2": [
        "example2"
    ]
}
var keys = Object.keys( obj ).sort();
var klen = keys.length;
for( var idx = 0; idx < klen; idx++ ){
    console.log( obj[ keys[ idx ] ] )
}

Upvotes: 7

Beri
Beri

Reputation: 11600

Probably duplicate of https://stackoverflow.com/questions/5906061/can-i-make-jquerys-each-method-iterate-in-reverse:

Sulution:

$.each(array.reverse(), function() { ... });

Upvotes: 0

Related Questions