Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按标识符对组数组使用reduce,JavaScript_Javascript_Reduce - Fatal编程技术网

按标识符对组数组使用reduce,JavaScript

按标识符对组数组使用reduce,JavaScript,javascript,reduce,Javascript,Reduce,我有一件东西应该扔掉 var arraJson = [ { "name" : "a", "department: "a" }, { "name" : "b", "department: "a" }, { "name" : "c", "department: &q

我有一件东西应该扔掉

var arraJson = [
{
    "name" : "a",
    "department: "a"
},
{
    "name" : "b",
    "department: "a"
},
{
    "name" : "c",
    "department: "b"
}]
预期产量

output = [
{
    "department" : "a",
    "employee" ["a","b"]
},
{
    "department" : "b",
    "employee" ["c"]
}
]

您可以执行以下操作:

var arraJson=[
{
“姓名”:“a”,
“部门”:“a”
},
{
“名称”:“b”,
“部门”:“a”
},
{
“姓名”:“c”,
“部门”:“b”
}]
res=arraJson.reduce((上一个,当前)=>{
让索引=上一个findIndex(item=>item.department==当前部门);
如果(索引>-1){
prev[index].employee.push(当前名称);
}否则{
上推送({'department':curr.department,'employee':[curr.name]});
}
返回上一个;
}, []);
控制台日志(res)