diff --git a/op_array/util.go b/op_array/util.go index 2c3627b..0137d0f 100644 --- a/op_array/util.go +++ b/op_array/util.go @@ -72,6 +72,16 @@ func Group[Key comparable, Value any](dataList []Value, keyFormat func(item Valu return res } +// GroupToMap 数组数据按照指定 key 进行分组 +func GroupToMap[Key comparable, Value any](dataList []Value, keyFormat func(item Value) Key) map[Key][]Value { + dataTable := make(map[Key][]Value) + for _, item := range dataList { + key := keyFormat(item) + dataTable[key] = append(dataTable[key], item) + } + return dataTable +} + // TreeItem 数据结构 type TreeItem[ID comparable, Value any] struct { ID ID `json:"id" dc:"数据ID"`