{ "type": "NORMAL", "var": { "id": 2808147, "name": "A相电压", "unit": "kV", "type": "Analog", "sn": "Ahhy_CPZX_G101__YC000", "device_sn": "Ahhy_CPZX_G101", "device_name": "进线", "parent_index": "Ahhy", "zero_meaning": null, "one_meaning": null, "var_code": "Ua", "record_period": "5分钟", "rw": 1 }, "name": "A相电压", "unit": "kV", "time_keys": [ "2023-05-25 00:00:00.000", "2023-05-25 00:05:00.000", "2023-05-25 00:10:00.000", "2023-05-25 00:15:00.000", "2023-05-25 00:20:00.000" ], "datas": [ 6.079, 6.079, 6.079, 6.079, 6.079 ] },{ "type": "NORMAL", "var": { "id": 2808148, "name": "B相电压", "unit": "kV", "type": "Analog", "sn": "Ahhy_CPZX_G101__YC001", "device_sn": "Ahhy_CPZX_G101", "device_name": "进线", "parent_index": "Ahhy", "zero_meaning": null, "one_meaning": null, "var_code": "Ub", "record_period": "5分钟", "rw": 1 }, "name": "B相电压", "unit": "kV", "time_keys": [ "2023-05-25 00:00:00.000", "2023-05-25 00:05:00.000", "2023-05-25 00:10:00.000", "2023-05-25 00:15:00.000", "2023-05-25 00:20:00.000" ], "datas": [ 6.079, 6.079, 6.064, 6.074, 6.065 ] } 以上为返回的数据格式,如何通过js处理成 ,时间 A相电压 B相电压这种表格形式
时间: 2023-06-28 16:13:54 浏览: 132
可以使用JavaScript的数组和循环来处理数据并生成表格。以下是一个示例代码:
```javascript
let data = [
{
"type": "NORMAL",
"var": {
"id": 2808147,
"name": "A相电压",
"unit": "kV",
"type": "Analog",
"sn": "Ahhy_CPZX_G101__YC000",
"device_sn": "Ahhy_CPZX_G101",
"device_name": "进线",
"parent_index": "Ahhy",
"zero_meaning": null,
"one_meaning": null,
"var_code": "Ua",
"record_period": "5分钟",
"rw": 1
},
"name": "A相电压",
"unit": "kV",
"time_keys": [
"2023-05-25 00:00:00.000",
"2023-05-25 00:05:00.000",
"2023-05-25 00:10:00.000",
"2023-05-25 00:15:00.000",
"2023-05-25 00:20:00.000"
],
"datas": [
6.079,
6.079,
6.079,
6.079,
6.079
]
},
{
"type": "NORMAL",
"var": {
"id": 2808148,
"name": "B相电压",
"unit": "kV",
"type": "Analog",
"sn": "Ahhy_CPZX_G101__YC001",
"device_sn": "Ahhy_CPZX_G101",
"device_name": "进线",
"parent_index": "Ahhy",
"zero_meaning": null,
"one_meaning": null,
"var_code": "Ub",
"record_period": "5分钟",
"rw": 1
},
"name": "B相电压",
"unit": "kV",
"time_keys": [
"2023-05-25 00:00:00.000",
"2023-05-25 00:05:00.000",
"2023-05-25 00:10:00.000",
"2023-05-25 00:15:00.000",
"2023-05-25 00:20:00.000"
],
"datas": [
6.079,
6.079,
6.064,
6.074,
6.065
]
}
];
let tableData = [['时间', 'A相电压', 'B相电压']];
// 循环处理数据
for (let i = 0; i < data[0].time_keys.length; i++) {
let row = [
data[0].time_keys[i],
data[0].datas[i],
data[1].datas[i]
];
tableData.push(row);
}
// 生成表格
let table = document.createElement('table');
for (let i = 0; i < tableData.length; i++) {
let tr = document.createElement('tr');
for (let j = 0; j < tableData[i].length; j++) {
let td = document.createElement('td');
td.textContent = tableData[i][j];
tr.appendChild(td);
}
table.appendChild(tr);
}
// 将表格插入到页面中
document.body.appendChild(table);
```
这段代码会生成一个包含时间、A相电压、B相电压三列的表格,并将数据填充进去。你可以将其中的`data`变量替换为你实际的数据,然后将表格插入到你想要的位置。
阅读全文