Files
lo/docs/data/core-map.md
T
Nathan Baulch e7386d9246 lint: fix inconsistent callback function parameter names (#730)
* Fix linting

* Use is.ElementsMatch

This will ignore the ordering of the final intersection. Especially
important when checking old versions of go that do not guarantee an order
when iterating through maps.

* lint: fix inconsistent callback function parameter names

* lint: rename "iteratee" to "transform" for *Map helpers

* lint: rename "project" parameters to "transform"

* lint: rename "cb" parameters to "callback"

* lint: rename "iteratee" to "callback" for ForEach helpers

---------

Co-authored-by: Franky W. <frankywahl@users.noreply.github.com>
Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-11-06 18:05:11 +01:00

1.3 KiB

name, slug, sourceRef, category, subCategory, playUrl, similarHelpers, variantHelpers, position, signatures
name slug sourceRef category subCategory playUrl similarHelpers variantHelpers position signatures
Map map slice.go#L26 core slice https://go.dev/play/p/OkPcYAhBo0D
core#slice#filtermap
core#slice#flatmap
core#slice#uniqmap
core#slice#rejectmap
core#slice#mapkeys
core#slice#mapvalues
core#slice#mapentries
core#slice#maptoslice
core#slice#filtermaptoslice
parallel#slice#map
mutable#slice#map
core#slice#map
10
func Map[T any, R any](collection []T, transform func(item T, index int) R) []R

Transforms each element in a slice to a new type using a function. Takes both the element and its index, making it useful for transformations that need positional context.

// Basic type transformation
transformed := lo.Map([]int64{1, 2, 3, 4}, func(x int64, index int) string {
    return strconv.FormatInt(x, 10)
})
// transformed: []string{"1", "2", "3", "4"}
// Transforming structs
type Person struct {
    FirstName string
    LastName  string
    Age       int
}

people := []Person{
    {FirstName: "John", LastName: "Doe", Age: 25},
    {FirstName: "Jane", LastName: "Smith", Age: 30},
}

fullNames := lo.Map(people, func(p Person, index int) string {
    return fmt.Sprintf("%s %s", p.FirstName, p.LastName)
})
// fullNames: []string{"John Doe", "Jane Smith"}