underscore
GitHubToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeBack to homepage

Group by

GroupBy splits a slice into a map[K][]V grouped by the result of the iterator function.

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}}
}