标题:复杂数组的es6 reduce处理方法
const list=[
{
className:'苹果',pev:100
},
{
className:'苹果',pev:103
},
{
className:'香蕉',pev:108
},
{
className:'香蕉',pev:105
},
{
className:'菠萝',pev:106
},
{
className:'菠萝',pev:109
}
]
let arr1=[]
list.reduce((index,item)=>{
const key=arr1.findIndex((val)=>{
return val.className==item.className
})
if(key>-1){
arr1.some(val=>{
val.className == item.className?val.pev.push(item.pev):''
})
}else{
arr1.push({
className:item.className,
pev:[item.pev]
})
}
},[])
console.log(arr1)