不知道写的对不对...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const arr1 = [{ name: '省级校验不合规', value: 0, type: 'top_1' },
{ name: '行政处罚修复', value: 100000, type: 'top_1' },
{ name: '浙江迟报', value: 200, type: 'top_1' },
{ name: '新行政处罚', value: 0, type: 'top_1' },
{ name: '国家校验不合规', value: 0, type: 'top_1' },
{ name: '省级校验不合规', value: 100, type: 'top_2' },
{ name: '新行政许可', value: 200000, type: 'top_2' },
{ name: '国家校验不合规', value: 500, type: 'top_2' },
{ name: '浙江迟报', value: 0, type: 'top_2' }]
const arr2 = [{ name: '省级校验不合规', number1: 0, number2: 100 },
{ name: '行政处罚修复', number1: 100000, number2: 0 },
{ name: '浙江迟报', number1: 200, number2: 0 },
{ name: '新行政处罚', number1: 0, number2: 0 },
{ name: '国家校验不合规', number1: 0, number2: 500 },
// { name: '新行政许可', number1: 2000, number2: 0 }, 兄弟怀疑你给的数据不太对
{ name: '新行政许可', number1: 0, number2: 200000 }]
function format(arr) {
let res = [];
for (const item of arr) {
let resItem = res.find(i => i.name === item.name)
if (!resItem) {
resItem = {
name: item.name,
number1: 0,
number2: 0
}
res.push(resItem)
}
resItem[`number${item.type.split('_').pop()}`] = item.value
}
return res;
}
console.log(JSON.stringify(format(arr1)));
console.log(JSON.stringify(arr2));
console.log(JSON.stringify(format(arr1)) === JSON.stringify(arr2));
</script>
</body>
</html>