diff --git a/array.go b/array.go index 9604988..6127d94 100644 --- a/array.go +++ b/array.go @@ -10,6 +10,7 @@ package wrapper import ( "encoding/json" "git.zhangdeman.cn/zhangdeman/op_type" + "github.com/tidwall/gjson" "reflect" "strings" ) @@ -142,3 +143,24 @@ func (at *Array[Bt]) ToStringWithSplit(split string) StringResult { Err: nil, } } + +// ExtraField 提取[]map/[]struct 中的指定字段, 并以list形式返回 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:00 2024/10/13 +func (at *Array[Bt]) ExtraField(fieldName string) *Array[any] { + if at.IsNil() { + return ArrayType[any]([]any{}) + } + byteData, _ := json.Marshal(at.value) + res := make([]any, 0) + list := gjson.ParseBytes(byteData).Array() + for _, item := range list { + itemValue := item.Get(fieldName) + if itemValue.Exists() { + res = append(res, itemValue.Value()) + } + } + return ArrayType(res) +}