使用reduce进行数组对象去重
const list=[{name: "阎成文",grpname: "党组巡视工作办公室"},{name: "123",grpname: "党组巡视工作办公室"}]
this.dateList(list)
dateList=(list)=>{
let dataList = [];
let obj = {};
// 处理第一层去重
dataList = list.reduce((cur, next) => {
let nextObj = {list:[]};
nextObj.name = next.grpname;
obj[nextObj.name] ? '' : (obj[nextObj.name] = true && cur.push(nextObj));
return cur;
}, []);
// 处理第二层变格式
for(let i=0;i<list.length;i++){
let index = dataList.findIndex((student)=>{
let objChild={};
if(student.name==list[i].grpname){
student.list.push(list[i])
}
})
}
this.setState({
dataList:dataList
})
},
]
}
// 最后处理为为
dataList=[
{
name: '党组巡视工作办公室',
list: [
{
name: '阎成文',
},
{
name: '123',
},
],