underscore/docs/content/collections/unique_by.md
Ruidy 1031038d42
feat: add Chunk, ContainsBy, UniqueBy, ParallelMap, map helpers
- Add `Chunk` to split slices into groups of size n.
- Add `ContainsBy` for predicate-based containment checks.
- Add `UniqueBy` to deduplicate slices by key selector.
- Add `ParallelMap` for concurrent mapping with context and error
handling.
- Add `maps.Keys` and `maps.Values` helpers for extracting map
keys/values.
- Update README and docs for new features.
- Refactor `Contains` to use `slices.Contains`.
2025-09-01 18:03:38 -04:00

483 B

title date
UniqueBy 2025-09-01T00:00:00-00:00

UniqueBy returns a duplicate-free version of the slice using a key selector. Order is preserved; the first occurrence of each key is kept.

package main

import (
 "fmt"
 u "github.com/rjNemo/underscore"
)

type User struct{ ID int; Email string }

func main() {
 users := []User{{1, "a@x"}, {2, "b@x"}, {3, "a@x"}}
 fmt.Println(u.UniqueBy(users, func(u User) string { return u.Email }))
 // [{1 a@x} {2 b@x}]
}