Skip to content

Commit ad993dc

Browse files
authored
Merge pull request #30 from hatena/PickByKeys
PickByKeys を追加
2 parents 9c88baf + 19b66ad commit ad993dc

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

godash.go

+5
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,8 @@ func Associate[T any, K comparable, V any](collection []T, transform func(item T
110110
func FlatMap[T any, R any](collection []T, iteratee func(item T, index int) []R) []R {
111111
return lo.FlatMap(collection, iteratee)
112112
}
113+
114+
// PickByKeys returns same map type filtered by given keys.
115+
func PickByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map {
116+
return lo.PickByKeys(in, keys)
117+
}

godash_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,10 @@ func ExampleFlatMap() {
258258
}))
259259
// Output: [0 1 2 3 4 5 6 7]
260260
}
261+
262+
func ExamplePickByKeys() {
263+
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
264+
result := godash.PickByKeys(kv, []string{"foo", "baz"})
265+
fmt.Printf("%v", result)
266+
// Output: map[baz:3 foo:1]
267+
}

0 commit comments

Comments
 (0)