mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
1.3 KiB
1.3 KiB
name, slug, sourceRef, category, subCategory, signatures, variantHelpers, similarHelpers, position
| name | slug | sourceRef | category | subCategory | signatures | variantHelpers | similarHelpers | position | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| FilterKeys | filterkeys | it/map.go#L238 | it | map |
|
|
|
56 |
Filters map keys based on a predicate function that takes both key and value. Returns a slice of keys that satisfy the predicate.
m := map[string]int{
"apple": 3,
"banana": 5,
"cherry": 2,
"date": 0,
}
result := it.FilterKeys(m, func(key string, value int) bool {
return value > 2
})
// []string{"apple", "banana"} (keys with values > 2)
numberMap := map[int]string{1: "one", 2: "two", 3: "three", 4: "four"}
result = it.FilterKeys(numberMap, func(key int, value string) bool {
return len(value) == 3
})
// []int{1, 2, 3} (keys where value length is 3)
personMap := map[string]int{"alice": 25, "bob": 30, "charlie": 17}
result = it.FilterKeys(personMap, func(key string, age int) bool {
return strings.HasPrefix(key, "a") && age >= 20
})
// []string{"alice"} (keys starting with "a" and age >= 20)
emptyMap := map[string]int{}
result = it.FilterKeys(emptyMap, func(key string, value int) bool {
return true
})
// []string{} (empty map)