简体   繁体   中英

JS: Ordered iteration of object

There is an object like this...

{
  2016: {
    3 : [
      { field: "content 1" }
      { field: "content 2" }
    ]
    10 : [
      { field: "content 3" }
    ]
  }
  2017: {
    8 : [
      { field: "content 4" }
    ]
  }
}

...and I need to get access to the subelements in an ascending order. That means I want to process 2016 object first, then 2017 object.

Within that I need to process the month objects also in ascending order.

Iteration like...

for (var year in map) {
    if (map.hasOwnProperty(year)) {
        console.log(year)
    }
}

won't do the job properly.

To get an ordered array of the content, recursively iterate the object, while getting the data by sorted keys, and using Array#concat to flatten the array.

 var data = {"2016":{"3":[{"field":"content 1"},{"field":"content 2"}],"10":[{"field":"content 3"}]},"2017":{"8":[{"field":"content 4"}]}}; function iterateByOrder(data) { var sorterKeys = Object.keys(data).sort(function(a, b) { return a - b; // sort by converting the keys to numbers }); return [].concat.apply([], sorterKeys.map(function(key) { // mapping the propeties to values, and flatting sub arrays return typeof data[key] === 'object' ? iterateByOrder(data[key]) : data[key]; })); } var result = iterateByOrder(data); console.log(result); 

We'll write a little analog of Array#forEach , which iterates over the key/value pairs in an object in sorted order, and calls a function on each pair, passing it the key and its value:

function forEach(object, fn) {
  object.entries() . sort((a, b) => a[0] - b[0]) . forEach(pair => fn(...pair));
}

If you don't have Object#entries , the write it yourself:

function objectEntries(object) {
  return Object.keys(object) . map(key => [key, object[key]]);
}

Now to iterate over your object:

forEach(map, (year, yearValue) => 
  forEach(yearValue, (month, monthValue) => 
    console.log(`At ${year}/${month}, got data ${monthValue}`)));

Javascript objects aren't ordered, so the first thing you will need to do is to grab the keys, sort them, and then iterate in that 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)}); for (var i = 0; i < sortedKeys.length; i++) { var key = sortedKeys[i]; console.log(data[key]); } 

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