map

Converts a slice of string-keyed maps into a single map keyed by the named field's value, turning a list you have to scan into a lookup table.

The field must hold a string, which becomes the key; elements missing that field are skipped, and when two elements share a key value the later one wins.

InputArgumentsReturns[]map[string]anystringmap[string]map[string]any
Source
BasicSource

Converts a slice of string-keyed maps into a single map keyed by the named field's value.

Given

{"users": [{"id": "u1", "name": "Alice"}, {"id": "u2", "name": "Bob"}]}

Template

{{ users | map:'id' }}

Result

{
  "u1": {
    "id": "u1",
    "name": "Alice"
  },
  "u2": {
    "id": "u2",
    "name": "Bob"
  }
}
By nameSource

Keys the slice by any field, here the name instead of the id.

Given

{"users": [{"id": "u1", "name": "Alice"}, {"id": "u2", "name": "Bob"}]}

Template

{{ users | map:'name' }}

Result

{
  "Alice": {
    "id": "u1",
    "name": "Alice"
  },
  "Bob": {
    "id": "u2",
    "name": "Bob"
  }
}
Duplicate keySource

Keeps the later element when two share a key value.

Given

{"users": [{"id": "u1", "name": "Alice"}, {"id": "u1", "name": "Bob"}]}

Template

{{ users | map:'id' }}

Result

{
  "u1": {
    "id": "u1",
    "name": "Bob"
  }
}
Missing fieldSource

Skips elements that lack the named field.

Given

{"users": [{"id": "u1", "name": "Alice"}, {"name": "Bob"}]}

Template

{{ users | map:'id' }}

Result

{
  "u1": {
    "id": "u1",
    "name": "Alice"
  }
}