Как создать динамический объект с динамическими ключами на основе значений массива - PullRequest
0 голосов
/ 17 апреля 2019

Я хочу создать объект на основе значений в массиве объекта, значения массива могут увеличиваться или уменьшаться в зависимости от ответа, поступающего от сервера, но значения в массиве в конечном итоге станут значениями динамического ключа. в объекте.

Я устал создавать объекты из массива объектов с использованием метода 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 } }
...