Back to Lo

Core Uniqmap

docs/data/core-uniqmap.md

1.53.0380 B
Original Source

Manipulates a slice and transforms it to a slice of another type with unique values.

go
type User struct {
    Name string
    Age  int
}

users := []User{{Name: "Alex", Age: 10}, {Name: "Alex", Age: 12}, {Name: "Bob", Age: 11}, {Name: "Alice", Age: 20}}

names := lo.UniqMap(users, func(u User, index int) string {
    return u.Name
})
// []string{"Alex", "Bob", "Alice"}