Remove duplicates from Array?

Hi Guys,
I think this topic was already answered, but I don’t get it. I want to loop through 2 ArrayLists, the Pages and then an JSON Array on the Markdown site:
thats what i get:
on page 1:

"metatags": [
    "Project",
    "Network",
    "Infrastructure"
],
on page 2:  
"metatags": [
    "todo",
    "organisation",
    "Infrastructure"
],

and then i add everything to an new ArrayList. Everything fine but how do i filter now for duplicates?
The Navbar will show now:

    Project
    Network
    Infrastructure
    todo
    organisation
    Infrastructure

But how can I delete 1 Infrastructure from the list? I think I can do it with another arraylist, which is working like a whitelist?

I googled a lot now and I think I need to do it with Scratch and isset, here’s my code:

{{ range .Pages }}
  {{ range .Params.metatags }}
    {{ $scratch := newScratch }}

    {{ $allcategorys := slice .}}
    {{ range $allcategorys }}
      {{ if not ( isset ($scratch ) . ) }}
        {{ $scratch.Set "category" . }}
        {{ $scratch.Get . }}
      {{ else }}
      {{ end }}
    {{ end }}

    <li uk-filter-control='[data-tag~="{{ $scratch.Get "category" }}"]'>
      <a href="index.html#">{{ $scratch.Get "category"}}</a>
    </li>
  {{ end }}
{{ end }}

I hope somebody of you can help me. :slight_smile:

1 Like

pseudocode (i.e. something like this)


{{/* Create a list of unique tags */}}

{{ $tags := (slice) }} 

{{ range .Pages }}
    {{ $tags = union $tags ( slice .Params.metatags ) }}
{{ end }}

{{/* Range over unique tags to make HTML */}}

{{ range .$tags }}
    {{/* HTML HERE */}}
{{ end }}

1 Like

Thank You, thats a good solution but it’s the method with a “whitelist”. Do you have an solution with the opportunity to sort it without a “new Array”, just with the existing items in .Params.metatags?

This will take a slice (array) and remove duplicates. but I’m not sure exactly what you are asking/trying to do. are you trying to create the HTML as you range through the lists? this will be fairly convoluted, and you will need to create an array (this could be in .Scratch or in a Go variable) for this (to store the unique tags).

1 Like

The current problem is. That i have duplicated values in an Array. This duplicated values are coming from an Array in the Markdown file. In the end I want to have an normal navbar without any duplicates. The reason for the duplicates are that the Markdown files have different metatags and I wanna sort these with the Items in the Navbar. That is working fine but like I said couple of the items are duplicated because they have the same “metatag” like others for example “Infrastructure”.
I want to import all Metatags from the different markdown files but they should display only once.

An image of the project here you can see what i mean:

the problem on your first solution is, that I want to import all items by the Markdown Files and I won’t fill up the whitelist Array ($tags on your solution) with keywords. The Navbar should be filled up automatically.

The uniq Tag changes anything :confused: I tried to put it before different variables.

Hi there. Try something like this. You can tweak it to your needs.

{{ $categories := slice }}
{{ range .Pages }}
  {{ $categories = $categories | append .Params.metatags }}
{{ end }}
{{ $categories }} <!-- not unique -->
{{ $categories | uniq }} <!-- unique -->

Which will output:

[Project Network Infrastructure todo organisation Infrastructure]
[Project Network Infrastructure todo organisation]
6 Likes

This worked for me perfect:

{{ $categories := slice }}
{{ range .Pages }}
  {{ $categories = $categories | append .Params.metatags }}
{{ end }}
{{ range ($categories | uniq) }}
  <li uk-filter-control='[data-tag~="{{ . }}"]'>
    <a href="index.html#">{{ . }}</a>
  </li>
{{ end }}

Thank You!

4 Likes