diff --git a/docs/slice.md b/docs/slice.md index afb148a..3e22335 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -37,6 +37,7 @@ import ( - [Filter](#Filter) - [Find](#Find) - [FindLast](#FindLast) +- [Flatten](#Flatten) - [FlattenDeep](#FlattenDeep) - [ForEach](#ForEach) @@ -546,6 +547,31 @@ func main() { +### Flatten +
Flatten slice with one level.
+ +Signature: + +```go +func Flatten(slice any) any +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + arr := [][][]string{{{"a", "b"}}, {{"c", "d"}}} + res := slice.Flatten(arr) + fmt.Println(res) //{{"a", "b"}, {"c", "d"}} +} +``` + + + ### FlattenDeepflattens slice recursive.
@@ -565,7 +591,7 @@ import ( func main() { arr := [][][]string{{{"a", "b"}}, {{"c", "d"}}} res := slice.FlattenDeep(arr) - fmt.Println(res) //[]string{"a", "b", "c", "d"} + fmt.Println(res) //{"a", "b", "c", "d"} } ``` diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index 380a6f7..9c81309 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -37,6 +37,7 @@ import ( - [Filter](#Filter) - [Find](#Find) - [FindLast](#FindLast) +- [Flatten](#Flatten) - [FlattenDeep](#FlattenDeep) - [ForEach](#ForEach) @@ -550,6 +551,30 @@ func main() { ``` +### Flatten +将切片压平一层
+ +函数签名: + +```go +func Flatten(slice any) any +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + arr := [][][]string{{{"a", "b"}}, {{"c", "d"}}} + res := slice.Flatten(arr) + fmt.Println(res) //{{"a", "b"}, {"c", "d"}} +} +``` + + ### FlattenDeepflattens slice recursive.