Reactjs 如何在React中将对象数组转换为对象
getquery graphql生成的api中的示例数据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
{
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 }) )));
这回答了你的问题吗?您是否从未拥有超过一个地区、一个地区和一个地点?