let data1={ detailList:[ { pspCode:"pspCode01", pspNoAbbr:"pspNoAbbr01", useRecordList:[ { ouput:10, upTime:"upTime01", downTime:"downTime01" } ] }, { pspCode:"pspCode02", pspNoAbbr:"pspNoAbbr02", useRecordList:[ { ouput:80, upTime:"upTime22", downTime:"downTime22" }, { ouput:30, upTime:"upTime23", downTime:"downTime23" }, { ouput:40, upTime:"upTime23", downTime:"downTime23" }, ] } ], unit:"unit" } let axisData=[] let max=0 for (let i = 0; i < data1.detailList.length; i++) { if (data1.detailList[i].pspCode) { axisData.push(data1.detailList[i].pspCode) }else{ axisData.push("") } if (data1.detailList[i].useRecordList.length>max) { max=data1.detailList[i].useRecordList.length } } let seriesData:any=[] for (let i = 0; i < max; i++) { seriesData.push( { name: "第"+(i+1)+"次上下机", type: "bar", barMaxWidth: "40", data: [], }) } console.log("X轴",axisData) for (let i = 0; i < data1.detailList.length; i++) { for (let k = 0; k < max; k++) { let arr=data1.detailList[i].useRecordList[k] if (data1.detailList[i].useRecordList[k]) { seriesData[k].data.push( { value:arr.ouput, upTime:arr.upTime, downTime:arr.downTime, } ) } } } data1.detailList.map((item,index)=>{ }) console.log("seriesData",seriesData)简化这段JS代码并且给出测试结果
时间: 2024-02-10 13:08:19 浏览: 70
Sourcebook:社会科学统计
可以将代码简化如下:
```js
let data1 = {
detailList: [
{
pspCode: "pspCode01",
pspNoAbbr: "pspNoAbbr01",
useRecordList: [
{
ouput: 10,
upTime: "upTime01",
downTime: "downTime01"
}
]
},
{
pspCode: "pspCode02",
pspNoAbbr: "pspNoAbbr02",
useRecordList: [
{
ouput: 80,
upTime: "upTime22",
downTime: "downTime22"
},
{
ouput: 30,
upTime: "upTime23",
downTime: "downTime23"
},
{
ouput: 40,
upTime: "upTime23",
downTime: "downTime23"
}
]
}
],
unit: "unit"
};
let axisData = data1.detailList.map(item => item.pspCode || "");
let max = Math.max(...data1.detailList.map(item => item.useRecordList.length));
let seriesData = Array.from({ length: max }, (_, index) => ({
name: `第${index + 1}次上下机`,
type: "bar",
barMaxWidth: "40",
data: []
}));
data1.detailList.forEach(detail => {
detail.useRecordList.forEach((record, index) => {
seriesData[index].data.push({
value: record.ouput,
upTime: record.upTime,
downTime: record.downTime
});
});
});
console.log("X轴", axisData);
console.log("seriesData", seriesData);
```
运行结果:
```
X轴 ["pspCode01", "pspCode02"]
seriesData [
{
name: '第1次上下机',
type: 'bar',
barMaxWidth: '40',
data: [ { value: 10, upTime: 'upTime01', downTime: 'downTime01' }, { value: 80, upTime: 'upTime22', downTime: 'downTime22' } ]
},
{
name: '第2次上下机',
type: 'bar',
barMaxWidth: '40',
data: [ { value: 30, upTime: 'upTime23', downTime: 'downTime23' } ]
},
{
name: '第3次上下机',
type: 'bar',
barMaxWidth: '40',
data: [ { value: 40, upTime: 'upTime23', downTime: 'downTime23' } ]
}
]
```
阅读全文