js { "18900": [ { "memory": "8.65%", //内存 "hardDisk": "17.7%", //硬盘 "cpu": "19.42%", //cpu "time": "2023-05-15 16:10:00" //时间点 } ], //服务器端口 "19000": [ { "memory": "11.94%", "hardDisk": "14.4%", "cpu": "12.5%", "time": "2023-05-15 16:10:00" } ] } 这是怎么使用for循环 在不知道有多少端口情况下
时间: 2023-06-28 21:06:53 浏览: 240
在存储过程中使用for循环
你可以使用对象的`keys()`方法获取对象的所有属性名,然后使用`forEach()`方法对每个端口进行循环。具体代码如下:
```javascript
const serverStatus = {
"18900": [
{
"memory": "8.65%",
"hardDisk": "17.7%",
"cpu": "19.42%",
"time": "2023-05-15 16:10:00"
}
],
"19000": [
{
"memory": "11.94%",
"hardDisk": "14.4%",
"cpu": "12.5%",
"time": "2023-05-15 16:10:00"
}
]
};
Object.keys(serverStatus).forEach(port => {
console.log(`Port ${port}:`);
serverStatus[port].forEach(status => {
console.log(` Memory usage: ${status.memory}`);
console.log(` Hard disk usage: ${status.hardDisk}`);
console.log(` CPU usage: ${status.cpu}`);
console.log(` Time: ${status.time}`);
});
});
```
输出结果如下:
```
Port 18900:
Memory usage: 8.65%
Hard disk usage: 17.7%
CPU usage: 19.42%
Time: 2023-05-15 16:10:00
Port 19000:
Memory usage: 11.94%
Hard disk usage: 14.4%
CPU usage: 12.5%
Time: 2023-05-15 16:10:00
```
阅读全文