Back to Lo

Core Groupby

docs/data/core-groupby.md

1.53.0293 B
Original Source

Groups elements by a key computed from each element. The result is a map keyed by the group key with slices of original elements.

go
groups := lo.GroupBy(
    []int{0, 1, 2, 3, 4, 5},
    func(i int) int {
        return i % 3
    },
)
// map[int][]int{0: {0, 3}, 1: {1, 4}, 2: {2, 5}}