使用Underscore.js将JSON对象响应合并为一个

时间:2015-12-23 07:15:53

标签: javascript json node.js youtube-api underscore.js

我尝试合并以下两个Youtube API响应的JSON对象。

我可能会有数百个视频(#1和#2的集合),我希望它们以下列格式合并为一个JSON。

  

*请忽略以下命名和语法,这里仅用于解释。

视频-1:{a:{..},b:{..}},视频-2:{a:{..},b:{..}},视频-3:{a: {..},b:{..}},...

只要第二个响应以设定顺序附加到第一个响应,我就开心了。 不喜欢

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

更像是

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

我已经尝试了

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

但它并没有以所需的方式合并。还试过_.flatten,_. union,_.defaults,但没有一个适合我。

a - 来自播放列表的视频片段

{ 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 - 视频内容详情

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

0 个答案:

没有答案