Reduce data into a different format and create markup from the new format

I have some data that looks like this:

		"name": "carrot",
		"type": "veg"
		"name": "raspberry",
		"type": "berry"
		"name": "apple",
		"type": "fruit"
		"name": "pear",
		"type": "fruit"

I would like to convert the data into just a set of the types so I can range over that set. In JS I’ve been doing something similar to this:

const typeSet = new Set()
array.forEach(item => typeSet.add(item.type))

Then creating the markup in JavaScript, but this is less than ideal

I’ve tried to use .Scratch.SetInMap to just use the map keys, but I’ve been limited by the scoping. Maybe I’ve just not been searching for the right thing and there is a way

Hi there,

Trying to understand here-- what output are you trying to achieve?

How are you using .Scratch?

Using range (untested):

{{ $types := slice }}
{{ range $data }}
  {{ $types = $types | append ( index . "type" ) }} <!-- get value of "type" -->
{{ end }}
{{ $types = $uniq $types }} <!-- get unique types values -->
1 Like

Yes, that works :smiley:

One Alteration is {{ $types = uniq $types }}

The .Scratch.SetInMap part is unimportant really, only a poor mans Set