lo
lo copied to clipboard
proposal: Another PickByKeys that ouputs a slice
Example
m := lo. PickByKeys(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"})
// []int{1,3}
The output values' order should be the same as the input slice.
Why we need it
Assume we have a list of openids:
openids = []string{openid1, openid2}
Then we have a method to transfer it into user_id:
func ToUserIDs(openids []string) map[string]string{}
The output of ToUserIDs
is a map, in which the key is openid and the value is UserID.
If I call Values
, then i would get a unordered list, which can not keep the original order with openids
So maybe we need one "Ordered PickByKeys function that returns a slice"?
Further more, the method could support the following scene:
gourpA := []string{openid1, openid2, openid3}
gourpB := []string{openid4, openid5, openid6}
gourpC := []string{openid7, openid8, openid9}
all := append([]string{}, gourpA...)
all := append(all, gourpB...)
all := append(all, gourpC...)
users := fetchUsers(all)
usersA := lo.PickByKeys(users, gourpA)
usersB := lo.PickByKeys(users, gourpB)
usersC := lo.PickByKeys(users, gourpC)
Join all the IDs, fetch in one request, then separate the returned entities into different groups.