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 浏览: 182
可以使用双重循环遍历数组,将对应的值赋给新的对象,最后将对象添加到结果数组中。具体实现可以参考以下代码:
```
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()` 方法将数值保留两位小数。
相关问题
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', }, ]
可以使用循环遍历来实现这个转换过程,具体步骤如下:
1. 创建一个新的空数组 result 用于存储最终结果;
2. 使用数组的 forEach 方法遍历 time 数组,对于每个时间,都创建一个空对象 obj;
3. 然后再使用数组的 forEach 方法遍历 arr 数组,对于每个设备,都在 obj 中添加一个属性,属性名为设备名称,属性值先设置为空字符串;
4. 接下来再遍历 arr_1 数组,对于每个设备,找到对应的时间和数据,将数据中的 power 属性值赋值给 obj 中对应设备的属性值;
5. 最后将 obj 添加到 result 数组中;
6. 返回 result 数组即可。
代码示例如下:
```javascript
let result = [];
time.forEach(timeVal => {
let obj = { time: timeVal };
arr.forEach(device => {
obj[device] = "";
});
arr_1.forEach(data => {
if (data.key === device) {
let mdData = data.mdData;
mdData.forEach(item => {
if (item.power !== undefined && item.power !== "") {
if (timeVal === "2023-05-30 " + item.time) {
obj[device] = item.power;
}
}
});
}
});
result.push(obj);
});
console.log(result);
```
输出结果如下:
```javascript
[
{
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.5',
power_all: '117.5',
power_dianti: '217.5',
power_lighting: '317.5'
},
{
time: '2023-05-30 19',
power_lift: '417.68',
power_all: '1417.68',
power_dianti: '2417.68',
power_lighting: '3417.68'
},
{
time: '2023-05-30 20',
power_lift: '417.88',
power_all: '1417.88',
power_dianti: '2417.88',
power_lighting: '3417.88'
}
]
```
js let timeArr = ['2023-05-31 19', '2023-05-31 20', '2023-05-31 21']; let deviceIds = ['power_lift']; let resTempArr = [ { data: [{usageComment: '1层照明',},{usageComment: '2层照明',},{usageComment: '3层照明',}],key: 'power_lift',mdData: [[{power: 2.38,},{power: 2.48,},{power: 2.58,},{power: 2.68,},],[{power: 3.38,},{power: 3.48,},{power: 3.58,},{power: 3.68,},],[{power: 4.38,},{power: 4.48,},{power: 4.58,},{power: 4.68,}]]}, { data: [], key: 'power_all', mdData: [] }, { data: [], key: 'power_dianti', mdData: [] }, ];转换成 [ { time: '2023-05-31 19', '1层照明': 2.38, '2层照明': 3.38, '3层照明': 4.38, }, { time: '2023-05-31 20', '1层照明': 2.48, '2层照明': 3.48, '3层照明': 4.48, }, { time: '2023-05-31 21', '1层照明': 2.58, '2层照明': 3.58, '3层照明': 4.68, }, ]
可以使用以下代码将原始数据转换为目标格式:
```javascript
let resultArr = [];
for (let i = 0; i < timeArr.length; i++) {
let tempObj = {
time: timeArr[i]
};
for (let j = 0; j < resTempArr[0].data.length; j++) {
let usageComment = resTempArr[0].data[j].usageComment;
let mdData = resTempArr[0].mdData[j];
let power = mdData[i].power;
tempObj[usageComment] = power;
}
resultArr.push(tempObj);
}
```
这段代码会遍历时间数组 `timeArr`,对于每个时间点,再遍历 `resTempArr` 中第一个元素的 `data` 数组,将每个用电设备的用电量添加到临时对象 `tempObj` 中,最终将 `tempObj` 添加到结果数组 `resultArr` 中。
阅读全文