You can do the following,
var arraJson = [
{
"name" : "a",
"department": "a"
},
{
"name" : "b",
"department": "a"
},
{
"name" : "c",
"department": "b"
}]
res = arraJson.reduce((prev, curr) => {
let index = prev.findIndex(item => item.department === curr.department);
if(index > -1) {
prev[index].employee.push(curr.name);
} else {
prev.push({ 'department': curr.department, 'employee': [curr.name] });
}
return prev;
}, []);
console.log(res);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…