Javascript Group By
...小于 1 分钟
Javascript Group By
Object 数组分组
下面的代码是用来按数组对象的指定属性来排序。
代码
export const groupBy = (arr: any[], key: string): any[] | null => {
if (arr) {
const grouped = arr.reduce((group: any, obj: any) => {
const data = obj[key]
group[data] = group[data] ?? []
group[data].push(obj)
return group
}, {})
return grouped
}
return null
}