Get most two recent items ordered by date from a dictionary array

Nie

New Member
#1
Been looking in similar threads but just can't to wrap my head around it. My current item seems way more complicated even for me. Here is my json (sorry it is a bit long):
Mã:
items = [
      {
        'name' : 'Books',
        'types':[
          {
              'name' : 'Hard Cover',
              'genres':[
                {
                  'genre' : 'Romance',

                  'added_date' : '2018-09-15',

                  'id':1
                },
                {
                  'genre' : 'Crime',

                  'added_date' : '2018-10-01',

                  'id' : 2
                }

              ],
              'cover':['pic1.png','pic2.png']
              },
              {
                'name' : 'Audio',
                'genres':[
                  {
                    'genre' : 'Memoir',

                    'added_date' : '2018-08-01',

                    'id' : 3
                  }

                ],
                'pictures':['pic3.png','pic4.png']
           },

         ]

      },

      {
        'name' : 'Videos',
        'types':[
          {
              'name' : 'English',
              'genres':[
                {
                  'genre' : 'Comedy',

                  'added_date' : '2018-10-14',

                  'id' : 12
                }

              ],
              'pictures':['pic5.png','pic6.png']
              }

         ]

      }
  ];
The output I want now is to get the 3 items which were added recently based on their added_date field in genres block. Now for those items, I need return an array with dictionary elements with full path:
Mã:
   [
    {'id':7,'genre':'Romance','name':'Hard Cover','added_date':'2018-09-16','top_parent_name':'Books'},
    {'id':8,'genre':'Memoir','name':'Audio','added_date':'2018-09-15','top_parent_name':'Books'},


     ]
Based on added_date, to get the latest 3 items that were added through out the whole object and its relavant information. I hope this makes sense.

So far, I have thought of the following but it quickly became cumbersome.
Mã:
items.forEach(function(value, index, array) {
      const types: any[] = value['types'];

      types.forEach(function(value_t, index_t, array_t){
        const genres: any[] = value_model['genes'];
       //loop again to get date....

      });



  });
 

Admin

Administrator
Thành viên BQT
#2
So if I make sense of the question correctly, you want the most recent 3 added by date, flattened into a particular format. If that is the case, then the answer will probably depend on how much data you are processing. One option is to build a fully flat array of your data, and then sort by date. For which I would use an array flattening utility function (i've included one, but use lodash if you can)

For example:
Mã:
const flatten = arr => arr.reduce((a, b) => a.concat(b), [])

const mapGenres = (topName, name, genres) => genres.map(genre => ({
  name,
  topName,
  ...genre
})

const result = flatten(items.map(({ name, types }) =>
  flatten(types.map(type => mapGenres(name, type.name, type.genres)))
))

result.sort((a, b) => return b.added_date.localeCompare(a.added_date))
This is untested, but should be fairly straight forward. Nested levels of flattening maps, producing your desired output at the leaves.
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top