flatten

Flattens a slice by exactly one level: any element that is itself a slice or array is spread into the result, and every other element is copied through unchanged.

A nested slice two levels deep still comes out as a slice, and an untyped nil element is dropped.

InputArgumentsReturns[]anyno arguments[]any
Source
BasicSource

Flattens a slice by exactly one level, spreading any element that is itself a slice into the result.

Given

{"groups": [["a", "b"], "c", ["d"]]}

Template

{{ groups | flatten }}

Result

[
  "a",
  "b",
  "c",
  "d"
]
Already flatSource

Leaves a slice with no nested slices unchanged.

Given

{"tags": ["go", "sintax", "docs"]}

Template

{{ tags | flatten }}

Result

[
  "go",
  "sintax",
  "docs"
]
Deeply nestedSource

Removes only one level, so a slice nested two deep stays a slice in the result.

Given

{"groups": [[["x"]], ["y"]]}

Template

{{ groups | flatten }}

Result

[
  [
    "x"
  ],
  "y"
]
EmptySource

Flattens an empty slice to an empty slice.

Given

{"groups": []}

Template

{{ groups | flatten }}

Result

[]