[5.4] Create a mapToGroups method#18949
Conversation
4312073 to
503d585
Compare
|
Seems neat. I have, on occasion, needed to use Also, the above example with Higher Order Messages can be written as: $users->groupBy("country")->map->pluck("id");Want to see something even crazier? Add a groupBy proxy and you can write it like this: $users->groupBy->country->map->map->map->id;Aside: |
I intentionally created a simple example. But if you do more complex stuff, you may end up with two closures. Additionally, if the value you're mapping to requires the original key, you're out of luck without this |
Yeah, I figured as much. Was just pointing it out.
Yeah. What I usually try to do is map first and include an extra entry for grouping if needed. But I always felt that it was a bit of a hack. Just depends on use case. |
|
I feel like this should follow the naming of |
|
@ConnorVG to me, $collection = collect([
'a' => [2, 5],
'b' => [8, 9],
]);
$collection->groupMap(function ($number) {
return $number * 2;
});
/*
[
'a' => [4, 10],
'b' => [16, 18],
]
*/...which would also maybe be a useful method, but this |
|
Eh? That's just a map map. |
|
I think we could add another test for numeric keys: $users->mapToGroups(function ($user) {
return [$user->age => $user->id];
});
/*
[
17 => [23, 1],
23 => [34, 3],
31 => [9],
44 => [4, 7, 2],
]
*/So we assert that it won't reindex the array in the future... |
503d585 to
1a36e74
Compare
Uh oh!
There was an error while loading. Please reload this page.