报价单
This commit is contained in:
@@ -18,6 +18,31 @@ func ArrayColumn[R any, T any](arr []T, column string) []R {
|
||||
return result
|
||||
}
|
||||
|
||||
func ArrayColumnTag[R any, T any](arrSrc []T, tag string) []R {
|
||||
var result []R
|
||||
arr := reflect.ValueOf(arrSrc)
|
||||
if arr.Len() == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
eleType := arr.Index(0).Elem().Type()
|
||||
|
||||
for j := 0; j < eleType.NumField(); j++ {
|
||||
if value, ok := eleType.Field(j).Tag.Lookup("json"); ok && value == tag {
|
||||
|
||||
for i := 0; i < arr.Len(); i++ {
|
||||
srcv := arr.Index(i)
|
||||
fv := srcv.Elem().Field(j)
|
||||
result = append(result, fv.Interface().(R))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func ArrayIndex[T any](arr []T, index int) (result T, ok bool) {
|
||||
if index < len(arr) {
|
||||
result = arr[index]
|
||||
|
||||
@@ -30,3 +30,13 @@ func TestArray2MapByKeyTag(t *testing.T) {
|
||||
log.Printf("%##v", a)
|
||||
log.Println(len(a))
|
||||
}
|
||||
|
||||
func TestArrayColumnTag(t *testing.T) {
|
||||
var abcs []*ABC = []*ABC{
|
||||
{1, "2", 3},
|
||||
{3, "1", 2},
|
||||
}
|
||||
a := ArrayColumnTag[string](abcs, "b")
|
||||
log.Printf("%##v", a)
|
||||
log.Println(len(a))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user