现在有这样一个需求,后台请求出来的人员名称是一个数组,需要把这些数组通过contestants_type进行归类在页面渲染,contestants_typ中只有一个负责人是确定的,其他类型通过后台进行配置的。
后台请求出来的数据:
我需要归类渲染的数据:
代码:
let person_list = []
_.each(res.data.contestants_data,function(item,index){
if(
!person_list.find((item2) => item2.name == item.contestants_type)
){
person_list.push({name:item.contestants_type,data:[item]})
}else{
_.each(person_list,function(item2,index2){
if(item2.name==item.contestants_type){
item2.data.push(item)
}
})
}
})
console.log(person_list)
此时打印出来的结果为: