diff --git a/docs/api/packages/slice.md b/docs/api/packages/slice.md index f8f3c751..5768ba20 100644 --- a/docs/api/packages/slice.md +++ b/docs/api/packages/slice.md @@ -86,6 +86,7 @@ import ( - [ToSlicePointer](#ToSlicePointer) - [Unique](#Unique) - [UniqueBy](#UniqueBy) +- [UniqueByField](#UniqueByField) - [Union](#Union) - [UnionBy](#UnionBy) - [UpdateAt](#UpdateAt) @@ -2312,6 +2313,47 @@ func main() { } ``` +### UniqueByField + +
根据struct字段对struct切片去重复。
+ +函数签名: + +```go +func UniqueByField[T any](slice []T, field string) ([]T, error) +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + type User struct { + ID int `json:"id"` + Name string `json:"name"` + } + + users := []User{ + {ID: 1, Name: "a"}, + {ID: 2, Name: "b"}, + {ID: 1, Name: "c"}, + } + + result, err := slice.UniqueByField(users, "ID") + if err != nil { + } + + fmt.Println(result) + + // Output: + // [{1 a} {2 b}] +} +``` + ### Union合并多个切片
@@ -2594,14 +2636,14 @@ import ( func main() { strs := []string{"a", "b", "a", "c", "d", "a"} - modifiedStrs, count := slice.SetToDefaultIf(strs, func(s string) bool { return "a" == s }) - + modifiedStrs, count := slice.SetToDefaultIf(strs, func(s string) bool { return "a" == s }) + fmt.Println(modifiedStrs) - fmt.Println(count) - + fmt.Println(count) + // Output: - // [ b c d ] - // 3 + // [ b c d ] + // 3 } ``` @@ -2657,11 +2699,11 @@ import ( ) func main() { - nums := []int{1, 2, 3, 4, 5} - padded := slice.RightPadding(nums, 0, 3) - fmt.Println(padded) - // Output: - // [1 2 3 4 5 0 0 0] + nums := []int{1, 2, 3, 4, 5} + padded := slice.RightPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [1 2 3 4 5 0 0 0] } ``` @@ -2684,10 +2726,10 @@ import ( ) func main() { - nums := []int{1, 2, 3, 4, 5} - padded := slice.LeftPadding(nums, 0, 3) - fmt.Println(padded) - // Output: - // [0 0 0 1 2 3 4 5] + nums := []int{1, 2, 3, 4, 5} + padded := slice.LeftPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [0 0 0 1 2 3 4 5] } ``` \ No newline at end of file diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index dc0a9643..7a53b5c0 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -86,6 +86,7 @@ import ( - [ToSlicePointer](#ToSlicePointer) - [Unique](#Unique) - [UniqueBy](#UniqueBy) +- [UniqueByField](#UniqueByField) - [Union](#Union) - [UnionBy](#UnionBy) - [UpdateAt](#UpdateAt) @@ -2310,6 +2311,47 @@ func main() { } ``` +### UniqueByField + +Remove duplicate elements in struct slice by struct field.
+ +Signature: + +```go +func UniqueByField[T any](slice []T, field string) ([]T, error) +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + type User struct { + ID int `json:"id"` + Name string `json:"name"` + } + + users := []User{ + {ID: 1, Name: "a"}, + {ID: 2, Name: "b"}, + {ID: 1, Name: "c"}, + } + + result, err := slice.UniqueByField(users, "ID") + if err != nil { + } + + fmt.Println(result) + + // Output: + // [{1 a} {2 b}] +} +``` + ### UnionCreates a slice of unique values, in order, from all given slices. using == for equality comparisons.