array

package
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 2 Imported by: 23

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyIn

func AnyIn[T comparable](vals []T, items []T) bool

AnyIn 判断 vals 中的任意元素是否在 items 中

func BuildMap

func BuildMap[T any, M any, K comparable](input []T, mapBuilder func(value T, i int) (K, M)) map[K]M

BuildMap 遍历 input 数组,使用 mapBuilder 函数(返回值为 Key, Value)创建 map

func Chunks added in v1.0.2

func Chunks[T any](data []T, size int) [][]T

Chunks 将数组分割成多个指定长度的数组

func ChunksEach added in v1.0.2

func ChunksEach[T any](data []T, size int, cb func(items []T))

ChunksEach 将数组分割成多个指定长度的数组,依次执行 cb 函数

func Diff

func Diff[T comparable](itemsA []T, itemsB []T) []T

Diff 提取 itemsA 中包含,但是 itemsB 中不存在的元素

func Difference

func Difference[T comparable](a, b []T) []T

Difference 取数据 a 和 b 的差集(返回在数据 a 中,但是不在 b 中的元素)

func Distinct

func Distinct[K comparable](input []K) []K

Distinct remove duplicate elements from array

func DistinctBy added in v1.0.3

func DistinctBy[K any, V comparable](input []K, keyFunc func(item K) V) []K

DistinctBy remove duplicate elements from array, use keyFunc to compare

func Each

func Each[T any](data []T, cb func(item T, index int))

Each 遍历data,依次执行 cb 函数

func Exclude

func Exclude[T comparable](items []T, excepts ...T) []T

Exclude exclude all items match excepts

func Filter

func Filter[T any](items []T, predicate func(item T, index int) bool) []T

Filter 字符串数组过滤

func FromMap

func FromMap[T any, K comparable](input map[K]T) []T

FromMap 从 map 中提取数组

func FromMapKeys

func FromMapKeys[T any, K comparable](input map[K]T) []K

FromMapKeys 将 map 的 key 转换为数组

func GroupBy

func GroupBy[T any, K comparable](data []T, cb func(item T) K) map[K][]T

GroupBy 按照数组的某个值进行分组

func In

func In[T comparable](val T, items []T) bool

In 判断元素是否在数组中

func Intersect

func Intersect[T comparable](arr1 []T, arr2 []T) []T

Intersect 两个数组取交集

func Map

func Map[T any, K any](items []T, mapper func(item T, index int) K) []K

Map 依次对每一个元素做 mapper 操作

func Reduce

func Reduce[T any, K any](data []K, cb func(carry T, item K) T, initValue T) T

Reduce 对数组执行 reduce 操作

func ReduceWithIndex added in v1.0.1

func ReduceWithIndex[T any, K any](data []K, cb func(carry T, item K, index int) T, initValue T) T

ReduceWithIndex 对数组执行 reduce 操作

func Repeat

func Repeat[T any](item T, count int) []T

Repeat 生成 count 个包含 item 值的数组

func RepeatFunc

func RepeatFunc[T any](fn func() T, count int) []T

RepeatFunc 重复执行 count 次 fn,返回结果为数组

func Reverse

func Reverse[T any](data []T) []T

Reverse 数组逆序

func Shuffle added in v1.0.2

func Shuffle[T any](data []T) []T

Shuffle 随机打乱数组,使用前请注意需要使用 rand.Seed(time.Now().UnixNano()) 方法来初始化随机数种子

func Sort

func Sort[T any](data []T, cb func(item1 T, item2 T) bool) []T

Sort 对数组进行排序

func ToMap

func ToMap[T any, K comparable](input []T, keyFunc func(T, int) K) map[K]T

ToMap 将数组转换为 map

func Union

func Union[T comparable](itemsA []T, itemsB []T) []T

Union 两个字符串数组合并,去重复

func Uniq added in v1.0.2

func Uniq[K comparable](input []K) []K

Uniq remove duplicate elements from array

func UniqBy added in v1.0.3

func UniqBy[K any, V comparable](input []K, keyFunc func(item K) V) []K

UniqBy remove duplicate elements from array, use keyFunc to compare

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL