diff --git a/op_array/util.go b/op_array/util.go index 29854f6..9efa97e 100644 --- a/op_array/util.go +++ b/op_array/util.go @@ -17,6 +17,16 @@ func ToMap[Key comparable, Value any](dataList []Value, keyFormat func(item Valu return res } +// ToCustomMap 数组转map +func ToCustomMap[Key comparable, Value any, CustomValue any](dataList []Value, keyFormat func(item Value) Key, formatCustomValue func(item Value) CustomValue) map[Key]CustomValue { + res := make(map[Key]CustomValue) + for _, item := range dataList { + key := keyFormat(item) + res[key] = formatCustomValue(item) + } + return res +} + // ExtractField 提取数组指定字段, 并构建成一个新的数组 func ExtractField[FieldValue any, Value any](dataList []Value, fieldValue func(item Value) FieldValue) []FieldValue { res := make([]FieldValue, 0)