Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: Another PickByKeys that ouputs a slice #467

Open
MakDon opened this issue Jun 18, 2024 · 0 comments
Open

proposal: Another PickByKeys that ouputs a slice #467

MakDon opened this issue Jun 18, 2024 · 0 comments

Comments

@MakDon
Copy link

MakDon commented Jun 18, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant