let arr=[["8461690.0", "2021-1"],["8425903.0", "2021-10"],["8458725.0", "2021-11"],["8438781.0", "2021-12"],["8351260.0", "2021-2"],["8491796.0", "2021-3"],["8517884.0", "2021-4"],["8519660.0", "2021-5"],["8505487.0", "2021-6"],["8438154.0", "2021-7"],["8374489.0", "2021-8"],["8453695.0", "2021-9"]],把arr转换成这样的数据格式:arr=[{type:2021-1和2021-2,value:841693.0,data:2021-1},{type:2021-1和2021-2,value:8351260.0,data:2021-2},{type:2021-2和2021-3,value:8351260.0,data:2021-2},{type:2021-2和2021-3,value:8491796.0,data:2021-3},]
时间: 2023-05-22 20:06:25 浏览: 114
请参考以下代码,将arr转换为目标数据格式:
```javascript
let arr=[["8461690.0", "2021-1"],["8425903.0", "2021-10"],["8458725.0", "2021-11"],["8438781.0", "2021-12"],["8351260.0", "2021-2"],["8491796.0", "2021-3"],["8517884.0", "2021-4"],["8519660.0", "2021-5"],["8505487.0", "2021-6"],["8438154.0", "2021-7"],["8374489.0", "2021-8"],["8453695.0", "2021-9"]];
let newData = [];
for (let i = 0; i < arr.length - 1; i++) {
let type = arr[i][1] + '和' + arr[i+1][1];
let value = parseFloat(arr[i][0]);
let data = arr[i][1];
newData.push({type: type, value: value, data: data});
}
console.log(newData);
```
输出结果为:
```javascript
[
{ type: '2021-1和2021-10', value: 8461690, data: '2021-1' },
{ type: '2021-10和2021-11', value: 8425903, data: '2021-10' },
{ type: '2021-11和2021-12', value: 8458725, data: '2021-11' },
{ type: '2021-12和2021-2', value: 8438781, data: '2021-12' },
{ type: '2021-2和2021-3', value: 8351260, data: '2021-2' },
{ type: '2021-3和2021-4', value: 8491796, data: '2021-3' },
{ type: '2021-4和2021-5', value: 8517884, data: '2021-4' },
{ type: '2021-5和2021-6', value: 8519660, data: '2021-5' },
{ type: '2021-6和2021-7', value: 8505487, data: '2021-6' },
{ type: '2021-7和2021-8', value: 8438154, data: '2021-7' },
{ type: '2021-8和2021-9', value: 8374489, data: '2021-8' }
]
```
希望这个答案可以满足您的需求。
阅读全文