Range Output Uniq String

i have an YAML file (articles.yaml) like this:

article1
   name: art1
   weight: "12"
   form: round
article2
   name: art2
   weight: "12"
   form: round
article3
   name: art3
   weight: "12"
   form: rectangle
article4
   name: art4
   weight: "12"
   form: rectangle

And i want to output just the form, but not duplicated.

{{ range $.Site.Data.articles }}
  - {{ .form | uniq }}
{{ end }}

Output:

  • round
  • rectangle

I understand that uniq doesnt work on strings. But what is the workaround for that problem?

You need to store the value of each .form first. Then execute uniq

{{ $form_values := slice -}}

{{ range $.Site.Data.articles }}
  {{ $form_values = $form_values | append .form }}
{{ end }}

{{ range ($form_values | uniq) }}
  {{ . }}
{{ end }}
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.