underscore/docs/content/collections/groupby.md
2023-06-07 14:49:55 +02:00

21 lines
414 B
Markdown

---
title: "Group by"
date: 2023-06-07T00:49:56+02:00
---
GroupBy splits a slice into a map[K][]V grouped by the result of the iterator function.
```go
package main
import (
"fmt"
u "github.com/rjNemo/underscore"
)
func main() {
nums := []float64{1.3, 2.1, 2.4}
groupingFunc := func(n float64) int { return int(math.Floor(n)) }
res := u.GroupBy(nums, groupingFunc) // { 1: {1.3}, 2: {2.1, 2.4}}
}
```