javascript - JS: Ordered iteration of object -
there object this...
{ 2016: { 3 : [ { field: "content 1" } { field: "content 2" } ] 10 : [ { field: "content 3" } ] } 2017: { 8 : [ { field: "content 4" } ] } } ...and need access subelements in ascending order. means want process 2016 object first, 2017 object.
within need process month objects in ascending order.
iteration like...
for (var year in map) { if (map.hasownproperty(year)) { console.log(year) } } won't job properly.
javascript objects aren't ordered, first thing need grab keys, sort them, , iterate in order.
data = { 2016: { 3 : [ { field: "content 1" }, { field: "content 2" }, ], 10 : [ { field: "content 3" }, ], }, 2017: { 8 : [ { field: "content 4" }, ], }, }; var keys = object.keys(data); var sortedkeys = keys.sort(function(a, b) {return parseint(a) - parseint(b)}); (var = 0; < sortedkeys.length; i++) { var key = sortedkeys[i]; console.log(data[key]); }
Comments
Post a Comment