mapjez

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package mapjez map相关函数

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteByValues

func DeleteByValues[K, V comparable](m map[K]V, values ...V)

DeleteByValues 通过value删除多个元素。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}
DeleteByValues(m, 1, 2)

fmt.Println(m)
Output:

map[c:3 d:4 e:5]

func DeleteFilter

func DeleteFilter[K comparable, V any](m map[K]V, iteratee func(key K, value V) bool)

DeleteFilter 遍历map,对每个元素调用 iteratee 函数,如果 iteratee 返回true,则删除该元素。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}
DeleteFilter(m, func(key string, value int) bool {
	return value%2 == 0
})

fmt.Println(m)
Output:

map[a:1 c:3 e:5 f:1]

func Deletes

func Deletes[K comparable, V any](m map[K]V, keys ...K)

Deletes 通过key删除多个元素。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}
Deletes(m, "a", "b")

fmt.Println(m)
Output:

map[c:3 d:4 e:5 f:1]

func Filter

func Filter[K comparable, V any](m map[K]V, iteratee func(key K, value V) bool) map[K]V

Filter 遍历map,对每个元素调用 iteratee 函数,如果 iteratee 返回 true,则将该元素添加到结果map中。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}

fmt.Println(Filter(m, func(_ string, value int) bool {
	return value%2 == 0
}))
Output:

map[b:2 d:4]

func ForEach

func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V))

ForEach 遍历map,对每个元素调用 iteratee 函数。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}

keys := make([]string, 0, len(m))
values := make([]int, 0, len(m))

ForEach(m, func(key string, value int) {
	keys = append(keys, key)
	values = append(values, value)
})

sort.Strings(keys)
sort.Ints(values)

fmt.Println(keys)
fmt.Println(values)
Output:

[a b c d e f]
[1 1 2 3 4 5]

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys 遍历map,将每个key添加到结果slice中。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}

k := Keys(m)
sort.Strings(k)

fmt.Println(k)
Output:

[a b c d e f]

func KeysAndValues

func KeysAndValues[K comparable, V any](m map[K]V) ([]K, []V)

KeysAndValues 返回map中所有的key和value。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}

keys, values := KeysAndValues(m)
sort.Strings(keys)
sort.Ints(values)

fmt.Println(keys)
fmt.Println(values)
Output:

[a b c d e f]
[1 1 2 3 4 5]

func KeysAndValuesFilter

func KeysAndValuesFilter[K comparable, V any](m map[K]V, iteratee func(key K, value V) bool) ([]K, []V)

KeysAndValuesFilter 遍历map,对每个元素调用 iteratee 函数,如果 iteratee 返回true,则将该元素添加到结果slice中。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}

keys, values := KeysAndValuesFilter(m, func(key string, value int) bool {
	return value%2 == 0
})
sort.Strings(keys)
sort.Ints(values)

fmt.Println(keys)
fmt.Println(values)
Output:

[b d]
[2 4]

func KeysBy

func KeysBy[K comparable, V any](m map[K]V, iteratee func(item K) K) []K

KeysBy 遍历map,对每个元素调用 iteratee 函数,并返回调用后结果。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}

kb := KeysBy(m, func(key string) string {
	return key + "1"
})
sort.Strings(kb)
fmt.Println(kb)
Output:

[a1 b1 c1 d1 e1 f1]

func MapToSliceBy

func MapToSliceBy[K comparable, V any, R any](m map[K]V, iteratee func(key K, value V) R) []R

MapToSliceBy map转切片,遍历map,对每个元素调用 iteratee 函数,并返回调用后结果切片。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}

list := MapToSliceBy(m, func(key string, value int) string {
	return key + " " + strconv.Itoa(value)
})
sort.Strings(list)

fmt.Println(list)
Output:

[a 1 b 2 c 3 d 4 e 5 f 1]

func MapToSliceFilter

func MapToSliceFilter[K comparable, V any, R any](m map[K]V, iteratee func(key K, value V) (R, bool)) []R

MapToSliceFilter map转切片,遍历map,对每个元素调用 iteratee 函数,如果 iteratee 返回true,则将该元素添加到结果切片中。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}

list := MapToSliceFilter(m, func(key string, value int) (string, bool) {
	return key + " " + strconv.Itoa(value), value%2 == 0
})
sort.Strings(list)

fmt.Println(list)
Output:

[b 2 d 4]

func ReplaceValue

func ReplaceValue[K, V comparable](m map[K]V, old, new V)

ReplaceValue 替换所有value等于 old 的元素。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
	"g": 1,
}
ReplaceValue(m, 1, 222)

fmt.Println(m)
Output:

map[a:222 b:2 c:3 d:4 e:5 f:222 g:222]

func Values

func Values[K comparable, V any](m map[K]V) []V

Values 返回map中所有的value。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}
v := Values(m)
sort.Ints(v)

fmt.Println(v)
Output:

[1 1 2 3 4 5]

func ValuesBy

func ValuesBy[K comparable, V any](m map[K]V, iteratee func(item V) V) []V

ValuesBy 遍历map,对每个元素调用 iteratee 函数,并返回调用后结果。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}

v := ValuesBy(m, func(value int) int {
	return value + 1
})
sort.Ints(v)

fmt.Println(v)
Output:

[2 2 3 4 5 6]

func ValuesUnique

func ValuesUnique[K, V comparable](m map[K]V) []V

ValuesUnique 返回map中所有的value,结果去重。

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 1,
}
v := ValuesUnique(m)
sort.Ints(v)
fmt.Println(v)
Output:

[1 2 3 4 5]

Types

This section is empty.

Jump to

Keyboard shortcuts

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