Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在React中将对象数组转换为对象_Reactjs_Typescript_Lodash_Codegen - Fatal编程技术网

Reactjs 如何在React中将对象数组转换为对象

Reactjs 如何在React中将对象数组转换为对象,reactjs,typescript,lodash,codegen,Reactjs,Typescript,Lodash,Codegen,getquery graphql生成的api中的示例数据 { id: '', name: '', regions: [ { id: '', name: '', districts: [ { id: '', name: '', locations: [{ id: '', city: '', division: '', str

getquery graphql生成的api中的示例数据

{
    id: '',
    name: '',
    regions: [
      {
        id: '',
        name: '',
        districts: [
          {
            id: '',
            name: '',
            locations: [{ id: '', city: '', division: '', street: '' }],
          },
        ],
      },
    ],
  }
将其转换为

{
  id: '',
  name: '',
  regions: {
    id: '',
    name: '',
    districts: {
      id: '',
      name: '',
      locations: { id: '', city: '', division: '', street: '' },
    },
  },
}
我已经尝试使用此代码转换它

const data = dataGetOrganization?.AllCountries

const result = Object.keys(data).reduce((acc, key) => {
  acc[key] = {
    ...data[key],
    _styles: {
      root: { overflow: 'hidden' },
    },
  }
  return acc
})

试着这样做:

var newObj = Object.assign({}, ...(array.map(item => ({ [item.key]: item.value }) )));

这回答了你的问题吗?您是否从未拥有超过一个地区、一个地区和一个地点?