js 将下面3个数组let time = ['2023-05-30 17', '2023-05-30 18', '2023-05-30 19', '2023-05-30 20']; let arr = ['power_lift', 'power_all', 'power_dianti', 'power_lighting']; let arr_1 = [ { key: "power_lift", mdData: [ {AQI: 1, power: 417.48},{AQI: 1, power: 417.5},{AQI: 1, power: 417.68},{AQI: 1, power: 417.88}] }, { key: "power_all", mdData: [ {AQI: 1, power: 117.48},{AQI: 1, power: 117.5},{AQI: 1, power: 1417.68},{AQI: 1, power: 1417.88}] }, { key: "power_dianti", mdData: [ {AQI: 1, power: 217.48},{AQI: 1, power: 217.5},{AQI: 1, power: 2417.68},{AQI: 1, power: 2417.88}] }, { key: "power_lighting", mdData: [ {AQI: 1, power: 317.48},{AQI: 1, power: 317.5},{AQI: 1, power: 3417.68},{AQI: 1, power: 3417.88}] }, ];转换成[ {time: '2023-05-30 17',power_lift: '417.48',power_all: '117.48',power_dianti: '217.48',power_lighting: '317.48',},{time: '2023-05-30 18',power_lift: '417.15',power_all: '117.5',power_dianti: '217.5',power_lighting: '317.5',},{time: '2023-05-30 19',power_lift: '417.68',power_all: '1417.48',power_dianti: '2417.48',power_lighting: '3417.48',},{time: '2023-05-30 20',power_lift: '417.88',power_all: '1417.48',power_dianti: '2417.48',power_lighting: '3417.48',}, ]
时间: 2024-02-25 15:53:39 浏览: 179
js获取时间
可以使用双重循环遍历数组,将对应的值赋给新的对象,最后将对象添加到结果数组中。具体实现可以参考以下代码:
```
let result = [];
for (let i = 0; i < time.length; i++) {
let obj = { time: time[i] };
for (let j = 0; j < arr.length; j++) {
let key = arr[j];
let data = arr_1.find(item => item.key === key).mdData[i];
obj[key] = data.power.toFixed(2);
}
result.push(obj);
}
console.log(result);
```
其中,使用 `toFixed()` 方法将数值保留两位小数。
阅读全文