From f3f064d064b5b7fbbc6ec5565e90d39e64a94ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 13 Oct 2024 19:23:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8F=90=E5=8F=96=E6=AF=8F?= =?UTF-8?q?=E4=B8=80=E9=A1=B9Item=E7=9A=84=E6=8C=87=E5=AE=9A=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E4=BD=9C=E4=B8=BAlist=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- array.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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) +}