let arr=[["8461690.0", "2021-1"] 1 : ["8425903.0", "2021-10"] 2 : ["8458725.0", "2021-11"] 3 : ["8438781.0", "2021-12"] 4 : ["8351260.0", "2021-2"] 5 : ["8491796.0", "2021-3"] 6 : ["8517884.0", "2021-4"] 7 : ["8519660.0", "2021-5"] 8 : ["8505487.0", "2021-6"] 9 : ["8438154.0", "2021-7"] 10 : ["8374489.0", "2021-8"] 11 : ["8453695.0", "2021-9"]],把a转换成这样的数据格式:a=[{type:2021-1和
时间: 2023-05-22 18:06:17 浏览: 118
["8461690.0"]}, {type:2021-2, value:["8351260.0"]}, {type:2021-3, value:["8491796.0"]}, {type:2021-4, value:["8517884.0"]}, {type:2021-5, value:["8519660.0"]}, {type:2021-6, value:["8505487.0"]}, {type:2021-7, value:["8438154.0"]}, {type:2021-8, value:["8374489.0"]}, {type:2021-9, value:["8453695.0"]}, {type:2021-10, value:["8425903.0"]}, {type:2021-11, value:["8458725.0"]}, {type:2021-12, value:["8438781.0"]}]。
以下是代码实现:
```
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 a = [];
for(let i=0; i<arr.length; i++){
let obj = {};
obj["type"] = arr[i][1];
obj["value"] = [arr[i][0]];
let found = false;
for(let j=0; j<a.length; j++){
if(a[j].type === arr[i][1]){
a[j].value.push(arr[i][0]);
found = true;
break;
}
}
if(!found){
a.push(obj);
}
}
console.log(a);
```
输出结果为:
```
[
{ type: '2021-1', value: [ '8461690.0' ] },
{ type: '2021-10', value: [ '8425903.0' ] },
{ type: '2021-11', value: [ '8458725.0' ] },
{ type: '2021-12', value: [ '8438781.0' ] },
{ type: '2021-2', value: [ '8351260.0' ] },
{ type: '2021-3', value: [ '8491796.0' ] },
{ type: '2021-4', value: [ '8517884.0' ] },
{ type: '2021-5', value: [ '8519660.0' ] },
{ type: '2021-6', value: [ '8505487.0' ] },
{ type: '2021-7', value: [ '8438154.0' ] },
{ type: '2021-8', value: [ '8374489.0' ] },
{ type: '2021-9', value: [ '8453695.0' ] }
]
```
阅读全文