简体   繁体   中英

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?

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.

eg:

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

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

Sulution:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM