const dataList = [{
"id": "4028e300786db35b01786db747720002",
"title": "專家職稱",
"name": "張飛",
"birthday": "1970-01-13",
"sex": "1",
"department": "兒科",
},
{
"id": "4028e300786db35b01786db747720003",
"title": "專家職稱",
"name": "關(guān)羽",
"birthday": "1970-01-13",
"sex": "1",
"department": "神經(jīng)外科",
},
{
"id": "4028e300786db35b01786db747720004",
"title": "專家職稱",
"name": "劉備",
"birthday": "1970-01-13",
"sex": "1",
"department": "普通外科",
},
{
"id": "4028e300786db35b01786db747720005",
"title": "專家職稱",
"name": "諸葛亮",
"birthday": "1970-01-13",
"sex": "1",
"department": "普通外科$兒科", //諸葛亮既屬于 普通外科$兒科
}
]
//將普通外科$兒科切割出來芍碧,形成一個新的對象
const result = {
data: dataList.reduce((res, item) => res.concat(...item.department.split('$').map(b => ({
...item,
b
}))), []),
};
//此時result:[兒科焕济,神經(jīng)外科,普通外科,兒科祥国,普通外科],然后數(shù)組對象根據(jù)某個key相同合并分組
function resultList(result) {
// console.log(result)
let obj = {};
result.forEach((item, index) => {
let = item;
if (!obj[b]) {
obj[b] = {
b,
data: []
}
}
obj[b].data.push(item);
})
let data = Object.values(obj)
console.log(data)
}
resultList(result.data)
最終想要的理想數(shù)據(jù)
image.png