const groupBy = (array, key) => {
const groups = {}
array.forEach(function (o) {
const group = o[key]
groups[group] = groups[group] || []
groups[group].push(o)
})
return Object.values(groups)
}
const gridData =[
{
id: '22',
name: '张三',
code: '15'
},
{
id: '23',
name: '李四',
code: '15'
},
{
id: '24',
name: '李五',
code: '14'
}
]
const mergeList = groupBy(gridData, 'code')
console.log(mergeList)
返回值: [[{"id":"24","name":"李五","code":"14"}],[{"id":"22","name":"张三","code":"15"},{"id":"23","name":"李四","code":"15"}]]
简单易用,不懂请留言~