如何基于数组值使用动态键创建动态对象

时间:2019-04-17 20:32:57

标签: arrays typescript object

我想基于对象数组中的值创建对象,该数组值可以根据来自服务器的响应而增加或减少,但是数组中的值最终将成为动态键的值在对象中。

我已经厌倦了使用reduce来从对象数组中创建对象,并且效果很好,但是有更好的方法。

const apiObj = apiValues.reduce((obj, item) => {
  obj[item.url] = item
  return obj
}, {})

对象数组

const apiValues = [
  {
    url: 'v1/api/something', 
    results: [
      { firstName : 'John', middleName : 'something', lastName : 'Doe'},
      { firstName : 'John1', middleName : 'something', lastName : 'Doe1'},
      { firstName : 'John2', middleName : 'something', lastName : 'Doe2'},
      { firstName : 'John3', middleName : 'something', lastName : 'Doe3'},
      { firstName : 'John4', middleName : 'something', lastName : 'Doe4'},
      { firstName : 'John5', middleName : 'something', lastName : 'Doe5'},
    ], 
    totalRecords: 70
  } ,
  {
    url: 'v1/api/someMore', 
    results: [
      { firstName : 'Red', middleName : 'something', lastName : 'John'},
      { firstName : 'Red1', middleName : 'something', lastName : 'John2'},
      { firstName : 'Red2', middleName : 'something', lastName : 'John2'},
      { firstName : 'Red3', middleName : 'something', lastName : 'Doe3'},
      { firstName : 'Red4', middleName : 'something', lastName : 'John4'},
      { firstName : 'Red5', middleName : 'something', lastName : 'Doe5'},
    ], 
    totalRecords: 8
  }, 
  {
    url: 'v1/api/whichOne', 
    results: [
      { firstName : 'Red', middleName : 'something', lastName : 'John'},
      { firstName : 'Red1', middleName : 'something', lastName : 'John2'},
      { firstName : 'Red2', middleName : 'something', lastName : 'John2'},
      { firstName : 'Red3', middleName : 'something', lastName : 'Doe3'},
      { firstName : 'Red4', middleName : 'something', lastName : 'John4'},
      { firstName : 'Red5', middleName : 'something', lastName : 'Doe5'},
    ], 
    totalRecords: 5
  }, 
  {
    url: 'v1/api/anyOne', 
    results: [
      { firstName : 'Red', middleName : 'something', lastName : 'John'},
      { firstName : 'Red1', middleName : 'something', lastName : 'John2'},
      { firstName : 'Red2', middleName : 'something', lastName : 'John2'},
      { firstName : 'Red3', middleName : 'something', lastName : 'Doe3'},
      { firstName : 'Red4', middleName : 'something', lastName : 'John4'},
      { firstName : 'Red5', middleName : 'something', lastName : 'Doe5'},
    ], 
    totalRecords: 7
  }, 
  {
    url: 'v1/api/any2', 
    results: [
      { firstName : 'Red', middleName : 'something', lastName : 'John'},
      { firstName : 'Red1', middleName : 'something', lastName : 'John2'},
      { firstName : 'Red2', middleName : 'something', lastName : 'John2'},
      { firstName : 'Red3', middleName : 'something', lastName : 'Doe3'},
      { firstName : 'Red4', middleName : 'something', lastName : 'John4'},
      { firstName : 'Red5', middleName : 'something', lastName : 'Doe5'},
    ], 
    totalRecords: 5
  }, 
    {
    url: 'v1/api/someMoreAndMore', 
    results: [
      { firstName : 'Red', middleName : 'something', lastName : 'John'},
      { firstName : 'Red1', middleName : 'something', lastName : 'John2'},
      { firstName : 'Red2', middleName : 'something', lastName : 'John2'},
      { firstName : 'Red3', middleName : 'something', lastName : 'Doe3'},
      { firstName : 'Red4', middleName : 'something', lastName : 'John4'},
      { firstName : 'Red5', middleName : 'something', lastName : 'Doe5'},
    ], 
    totalRecords: 67
  }, 
];

预期结果

const apiObj = { 'v1/api/something':
   { url: 'v1/api/something',
     results:
      [ { firstName: 'John', middleName: 'something', lastName: 'Doe' },
  { firstName: 'John1', middleName: 'something', lastName: 'Doe1' },
  { firstName: 'John2', middleName: 'something', lastName: 'Doe2' },
  { firstName: 'John3', middleName: 'something', lastName: 'Doe3' },
  { firstName: 'John4', middleName: 'something', lastName: 'Doe4' },
  { firstName: 'John5', middleName: 'something', lastName: 'Doe5' }  ],
     totalRecords: 70 },
  'v1/api/someMore':
   { url: 'v1/api/someMore',
     results:
      [ { firstName: 'John', middleName: 'something', lastName: 'Doe' },
  { firstName: 'John1', middleName: 'something', lastName: 'Doe1' },
  { firstName: 'John2', middleName: 'something', lastName: 'Doe2' },
  { firstName: 'John3', middleName: 'something', lastName: 'Doe3' },
  { firstName: 'John4', middleName: 'something', lastName: 'Doe4' },
  { firstName: 'John5', middleName: 'something', lastName: 'Doe5' }  ],
     totalRecords: 8 },
  'v1/api/whichOne':
   { url: 'v1/api/whichOne',
     results:
      [ { firstName: 'John', middleName: 'something', lastName: 'Doe' },
  { firstName: 'John1', middleName: 'something', lastName: 'Doe1' },
  { firstName: 'John2', middleName: 'something', lastName: 'Doe2' },
  { firstName: 'John3', middleName: 'something', lastName: 'Doe3' },
  { firstName: 'John4', middleName: 'something', lastName: 'Doe4' },
  { firstName: 'John5', middleName: 'something', lastName: 'Doe5' }  ],
     totalRecords: 5 },
  'v1/api/anyOne':
   { url: 'v1/api/anyOne',
     results:
      [ { firstName: 'John', middleName: 'something', lastName: 'Doe' },
  { firstName: 'John1', middleName: 'something', lastName: 'Doe1' },
  { firstName: 'John2', middleName: 'something', lastName: 'Doe2' },
  { firstName: 'John3', middleName: 'something', lastName: 'Doe3' },
  { firstName: 'John4', middleName: 'something', lastName: 'Doe4' },
  { firstName: 'John5', middleName: 'something', lastName: 'Doe5' }  ],
     totalRecords: 7 },
  'v1/api/any2':
   { url: 'v1/api/any2',
     results:
      [ { firstName: 'John', middleName: 'something', lastName: 'Doe' },
  { firstName: 'John1', middleName: 'something', lastName: 'Doe1' },
  { firstName: 'John2', middleName: 'something', lastName: 'Doe2' },
  { firstName: 'John3', middleName: 'something', lastName: 'Doe3' },
  { firstName: 'John4', middleName: 'something', lastName: 'Doe4' },
  { firstName: 'John5', middleName: 'something', lastName: 'Doe5' }  ],
     totalRecords: 5 },
  'v1/api/someMoreAndMore':
   { url: 'v1/api/someMoreAndMore',
     results:
      [ { firstName: 'John', middleName: 'something', lastName: 'Doe' },
  { firstName: 'John1', middleName: 'something', lastName: 'Doe1' },
  { firstName: 'John2', middleName: 'something', lastName: 'Doe2' },
  { firstName: 'John3', middleName: 'something', lastName: 'Doe3' },
  { firstName: 'John4', middleName: 'something', lastName: 'Doe4' },
  { firstName: 'John5', middleName: 'something', lastName: 'Doe5' }  ],
     totalRecords: 67 } }

0 个答案:

没有答案