请小伙伴帮忙,关于数据格式转换的问题
var page_data = [{ "student": {
"user_id": "278842",
"user_realanme": "阅卷学生46",
"student_no": "210512046",
"cm_id": "9758",
"cm_name": "初一(22)班"
},
"exam": [{
"ze_id": "477",
"zs_id": "1754",
"ze_xueke_name": "语文",
"ze_xueke": "1",
"yxcjd_score_xs": "78.0",
"yxcjd_grade_sort": "2",
"yxcjd_cm_sort": "1"
}, {
"ze_id": "495",
"zs_id": "1768",
"ze_xueke_name": "数学",
"ze_xueke": "2",
"yxcjd_score_xs": "",
"yxcjd_grade_sort": "",
"yxcjd_cm_sort": ""
}, {
"ze_id": "483",
"zs_id": "1758",
"ze_xueke_name": "英语",
"ze_xueke": "3",
"yxcjd_score_xs": "51.0",
"yxcjd_grade_sort": "2",
"yxcjd_cm_sort": "1"
}]
}, {
"student": {
"user_id": "278802",
"user_realanme": "骆海亮",
"student_no": "210512006",
"cm_id": "9757",
"cm_name": "初一(21)班"
},
"exam": [{
"ze_id": "477",
"zs_id": "1754",
"ze_xueke_name": "语文",
"ze_xueke": "1",
"yxcjd_score_xs": "84.0",
"yxcjd_grade_sort": "1",
"yxcjd_cm_sort": "1"
}, {
"ze_id": "495",
"zs_id": "1768",
"ze_xueke_name": "数学",
"ze_xueke": "2",
"yxcjd_score_xs": "",
"yxcjd_grade_sort": "",
"yxcjd_cm_sort": ""
}, {
"ze_id": "483",
"zs_id": "1758",
"ze_xueke_name": "英语",
"ze_xueke": "3",
"yxcjd_score_xs": "56.0",
"yxcjd_grade_sort": "1",
"yxcjd_cm_sort": "1"
}]
}]
变成下面的格式 变成以学科为主的
var two= [{ name: '语文',
children: [
{ yxcjd_score_xs: 78, yxcjd_cm_sort: 1, yxcjd_grade_sort: 2 },
{ yxcjd_score_xs: 84, yxcjd_cm_sort: 1, yxcjd_grade_sort: 1 },
]
},
{
name: '数学',
children: [
{ yxcjd_score_xs: 78, yxcjd_cm_sort: 1, yxcjd_grade_sort: 2 },
{ yxcjd_score_xs: 84, yxcjd_cm_sort: 1, yxcjd_grade_sort: 1 },
]
},
{
name: '英语',
children: [
{ yxcjd_score_xs: 78, yxcjd_cm_sort: 1, yxcjd_grade_sort: 2 },
{ yxcjd_score_xs: 84, yxcjd_cm_sort: 1, yxcjd_grade_sort: 1 },
]
},
]
回答:
const convert = (data) => { return Object.values(data.reduce((result, item) => {
item.exam.forEach(exam => {
!result[exam.ze_xueke_name] && (result[exam.ze_xueke_name] = {
name: exam.ze_xueke_name,
children: []
})
result[exam.ze_xueke_name].children.push({
yxcjd_score_xs: exam.yxcjd_score_xs,
yxcjd_cm_sort: exam.yxcjd_cm_sort,
yxcjd_grade_sort: exam.yxcjd_grade_sort
})
})
return result
}, {}))
}
土办法, 坐等其它处理方式
以上是 请小伙伴帮忙,关于数据格式转换的问题 的全部内容, 来源链接: utcz.com/p/935464.html