Merge JSON object response into one using Undersco

2019-08-30 11:55发布

问题:

I'm trying to merge the following two JSON objects that are Youtube API response.

I will likely have hundreds of videos (sets of #1 and #2) and I want them to be merged into one JSON in the following format.

*Please ignore naming and syntax in the following, they are just for explanation here.

Video-1: { a: {..}, b: {..} }, video-2: { a: {..}, b: {..} }, video-3: { a: {..}, b: {..} }, ...

As long as the second response is appended to the first one in the set order, I'm happy. Not like

{a, a, a, a, a, a},{b, b, b, b, b, b}

More like

{a,b}, {a,b}, {a,b}, {a,b}, {a,b}, {a,b}

I've tried

_.values(_.extend(_.indexBy(array1, 'kind'), _.indexBy(array2, 'kind')))

but it doesn't merge in the desired way. Also tried _.flatten, _.union, _.defaults but none of them work for me.

a - Video snippet from playlist

{ kind: 'youtube#playlistItem',
       etag: '"kuL0kDMAqRo3pU7O0pwlO-Lfzp4/tGxY5umSWnuSkk1adS4H8nsd7nI"',
       id: 'PLhAf_GEuRunTjOdIMNbFsPSMLcbkw3RoTqruH97SaMEs',
       snippet: 
        { publishedAt: '2015-12-13T12:16:47.000Z',
          channelId: 'UCwy6X3JB24VTsDFqMwdO5Jg',
          title: 'John Hockenberry: We are all designers',
          description: 'http://www.ted.com Journalist John Hockenberry tells a personal story inspired by a pair of flashy wheels in a wheelchair-parts catalogue -- and how they showed him the value of designing a life of intent. (From The Design Studio session at TED2012, guest-curated by Chee Pearlman and David Rockwell.)\n\nTEDTalks is a daily video podcast of the best talks and performances from the TED Conference, where the world\'s leading thinkers and doers give the talk of their lives in 18 minutes. Featured speakers have included Al Gore on climate change, Philippe Starck on design, Jill Bolte Taylor on observing her own stroke, Nicholas Negroponte on One Laptop per Child, Jane Goodall on chimpanzees, Bill Gates on malaria and mosquitoes, Pattie Maes on the "Sixth Sense" wearable tech, and "Lost" producer JJ Abrams on the allure of mystery. TED stands for Technology, Entertainment, Design, and TEDTalks cover these topics as well as science, business, development and the arts. Closed captions and translated subtitles in a variety of languages are now available on TED.com, at http://www.ted.com/translate\n\nIf you have questions or comments about this or other TED videos, please go to http://support.ted.com',
          thumbnails: 
           { default: 
              { url: 'https://i.ytimg.com/vi/ti_i-M3pk5M/default.jpg',
                width: 120,
                height: 90 },
             medium: 
              { url: 'https://i.ytimg.com/vi/ti_i-M3pk5M/mqdefault.jpg',
                width: 320,
                height: 180 },
             high: 
              { url: 'https://i.ytimg.com/vi/ti_i-M3pk5M/hqdefault.jpg',
                width: 480,
                height: 360 },
             standard: 
              { url: 'https://i.ytimg.com/vi/ti_i-M3pk5M/sddefault.jpg',
                width: 640,
                height: 480 },
             maxres: 
              { url: 'https://i.ytimg.com/vi/ti_i-M3pk5M/maxresdefault.jpg',
                width: 1280,
                height: 720 } },
          channelTitle: 'Seong Lee',
          playlistId: 'PLy7hFLa6kbitaVkEcgPEt6GlfVjuyO91K',
          position: 30,
          resourceId: { kind: 'youtube#video', videoId: 'ti_i-M3pk5M' } },
       contentDetails: { videoId: 'ti_i-M3pk5M' } },
}

b - Video content detail

{ kind: 'youtube#videoListResponse',
    etag: '"kuL0kDMAqRo3pU7O0pwlO-Lfzp4/BQsUkCeR_vYw5Am_P0zDQ65R2RI"',
    pageInfo: { totalResults: 1, resultsPerPage: 1 },
    items: 
     [ { kind: 'youtube#video',
         etag: '"kuL0kDMAqRo3pU7O0pwlO-Lfzp4/63882pNOoZMxxAveXDjjDGxrL6g"',
         id: 'ti_i-M3pk5M',
         contentDetails: 
          { duration: 'PT19M44S',
            dimension: '2d',
            definition: 'hd',
            caption: 'true',
            licensedContent: true } },
       [length]: 1 ] }