0

GetQuery

sample data from getquery graphql generated api

{
    id: '',
    name: '',
    regions: [
      {
        id: '',
        name: '',
        districts: [
          {
            id: '',
            name: '',
            locations: [{ id: '', city: '', division: '', street: '' }],
          },
        ],
      },
    ],
  }

convert it to

{
  id: '',
  name: '',
  regions: {
    id: '',
    name: '',
    districts: {
      id: '',
      name: '',
      locations: { id: '', city: '', division: '', street: '' },
    },
  },
}

I already tried to convert it by using this code

const data = dataGetOrganization?.AllCountries

const result = Object.keys(data).reduce((acc, key) => {
  acc[key] = {
    ...data[key],
    _styles: {
      root: { overflow: 'hidden' },
    },
  }
  return acc
})
Sahil Raj Thapa
  • 2,353
  • 3
  • 12
  • 23
  • 1
    Does this answer your question? [How do I convert array of Objects into one Object in JavaScript?](https://stackoverflow.com/questions/19874555/how-do-i-convert-array-of-objects-into-one-object-in-javascript) – RowanX Oct 13 '20 at 15:19
  • 1
    Do you never have more than one region, one district and one location? – ShamPooSham Oct 13 '20 at 15:36

1 Answers1

1

Try something like this:

var newObj = Object.assign({}, ...(array.map(item => ({ [item.key]: item.value }) )));
RowanX
  • 1,272
  • 2
  • 14
  • 27