let data = [ { companyId: "3d13ebe9cbc17427f4feac2bea8bb3b2", companyName: "中国石油华北石化分公司", companyNo: "2", refineries: [ { refineryId: "c32e5d49efe6be070a185b8eb2035963", refineryName: "炼油厂", refineryNo: "2-1", units: [ { unitId: "db17da36d7c479c97986b8c3eb5180e2", unitName: "160万吨/年重油催化装置", unitNo: "2-1-01-FCC" }, { unitId: "10ad58b3bba53db1317f5f1d113fd295", unitName: "340万吨/年渣油加氢处理装置", unitNo: "2-1-04-RFCC" } ] } ] } ] 将代码中所有包含Id的字段全部替换为id 将所有包含Name的字段全部替换为Name 将所有包含No的字段全部替换为No 将refineries和units替换为children
时间: 2023-02-07 14:23:11 浏览: 144
你可以使用以下代码来实现上述操作:
```
const data = [{
companyId: "3d13ebe9cbc17427f4feac2bea8bb3b2",
companyName: "中国石油华北石化分公司",
companyNo: "2",
refineries: [{
refineryId: "c32e5d49efe6be070a185b8eb2035963",
refineryName: "炼油厂",
refineryNo: "2-1",
units: [{
unitId: "db17da36d7c479c97986b8c3eb5180e2",
unitName: "160万吨/年重油催化装置",
unitNo: "2-1-01-FCC"
}, {
unitId: "10ad58b3bba53db1317f5f1d113fd295",
unitName: "340万吨/年渣油加氢处理装置",
unitNo: "2-1-04-RFCC"
}]
}]
}];
const newData = data.map(item => {
return {
id: item.companyId,
name: item.companyName,
no: item.companyNo,
children: item.refineries.map(refinery => {
return {
id: refinery.refineryId,
name: refinery.refineryName,
no: refinery.refineryNo,
children: refinery.units.map(unit => {
return {
id: unit.unitId,
name: unit.unitName,
no: unit.unitNo
};
})
};
})
};
});
console.log(newData);
```
这样就能将原来的数据结构中的所有`Id`、`Name`和`No`字段替换为小写的`id`、`name`和`no`,并将`refineries`和`units`替换为小写的`children`了。
阅读全文