const data = [ { regionName: "深圳市", regionLevel:"2", regionCode: "440300000000", childList: [ { regionName: "罗湖区", regionCode: "440303000000", regionLevel:"3", childList: [ { regionName: "罗湖区", regionCode: "440303000000", regionLevel:"3",} ] }, { regionName: "黄贝街道", regionCode: "440303002000", regionLevel:"3", }, ], }, ];中怎么把childList变为child
时间: 2024-02-02 16:04:08 浏览: 115
C++之Boost::array用法简介
可以使用 JavaScript 的 map 方法进行转换:
```
const data = [
{
regionName: "深圳市",
regionLevel: "2",
regionCode: "440300000000",
child: [
{
regionName: "罗湖区",
regionCode: "440303000000",
regionLevel: "3",
child: [
{
regionName: "罗湖区",
regionCode: "440303000000",
regionLevel: "3",
},
],
},
{
regionName: "黄贝街道",
regionCode: "440303002000",
regionLevel: "3",
},
],
},
];
const newData = data.map((item) => {
const { childList, ...rest } = item;
return { ...rest, child: childList };
});
console.log(newData);
```
运行结果:
```
[
{
regionName: '深圳市',
regionLevel: '2',
regionCode: '440300000000',
child: [
{
regionName: '罗湖区',
regionCode: '440303000000',
regionLevel: '3',
child: [ [Object] ]
},
{
regionName: '黄贝街道',
regionCode: '440303002000',
regionLevel: '3'
}
]
}
]
```
阅读全文