# How to select array element by element property

**URL:** <https://discourse.gohugo.io/t/how-to-select-array-element-by-element-property/33234>\
**Category:** support\
**Created:** [June 4, 2021, 6:13pm UTC](https://discourse.gohugo.io/t/how-to-select-array-element-by-element-property/33234 "2021-06-04T18:13:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kdawg1406](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@kdawg1406](https://discourse.gohugo.io/u/kdawg1406)\
**Post date:** [June 4, 2021, 6:13pm UTC](https://discourse.gohugo.io/t/how-to-select-array-element-by-element-property/33234/1 "2021-06-04T18:13:08Z")

</div>

I’ve spent an hour trying to do something so simple and need help, thank you in advance.

Given this Data file.

**data/eleders.yml**

```auto
elders:
  - elder_id: steve
    name: Steve Smith

  - elder_id: john
    name: John Smith

```

In my template, where .Params.elder\_id = “steve”

```auto
{{ $elder := index .Site.Data.elders .Params.elder_id }}

```

`$elder` contains the entire array and not the expected single map object with elder\_id = “steve”.

```auto
map[elders:[map[elder_id:steve name:Steve Smith] map[elder_id:john name:John Smith]]]

```

Can someone please help me understand how to select an array element (object) by key?

Thank you so much,

Karl

---

<div class="post-metadata">

**Author:** ![kdawg1406](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@kdawg1406](https://discourse.gohugo.io/u/kdawg1406)\
**Post date:** [June 4, 2021, 7:41pm UTC](https://discourse.gohugo.io/t/how-to-select-array-element-by-element-property/33234/2 "2021-06-04T19:41:33Z")

</div>

It finally came to me that this was an array of maps.

I’m using range first 1 to access the array and I know it will only return one array map.

```auto
{{ define "main" }}
{{ $data := index .Site.Data.elders }}
<section class="container bg-containerBackground py-5 prose md:prose-md px-3">
    {{ range first 1 (where $data.elders "id" .Params.elder_id) }}
    <p>{{ .id }}</p>
    {{ end }}
</section>
{{ end }}

```

Hope this helps someone.

Hugo newbie…

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [June 4, 2021, 7:46pm UTC](https://discourse.gohugo.io/t/how-to-select-array-element-by-element-property/33234/3 "2021-06-04T19:46:29Z")

</div>

Also note that the data file is the highest level object. Currently you have to use `.Site.Data.elders.elders` to get to the data. Do this instead:

data/elders.yml

```auto
- elder_id: steve
  name: Steve Smith
- elder_id: john
  name: John Smith

```

Also note that using `elder_id` in a data file named `elders` is redundant. Simplify:

```auto
- id: steve
  name: Steve Smith
- id: john
  name: John Smith

```

---

<div class="post-metadata">

**Author:** ![kdawg1406](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@kdawg1406](https://discourse.gohugo.io/u/kdawg1406)\
**Post date:** [June 4, 2021, 8:11pm UTC](https://discourse.gohugo.io/t/how-to-select-array-element-by-element-property/33234/4 "2021-06-04T20:11:02Z")

</div>

Thank you for the tip! Learning more every day!

---

<div class="post-metadata">

**Author:** ![kdawg1406](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@kdawg1406](https://discourse.gohugo.io/u/kdawg1406)\
**Post date:** [June 4, 2021, 10:06pm UTC](https://discourse.gohugo.io/t/how-to-select-array-element-by-element-property/33234/5 "2021-06-04T22:06:59Z")

</div>

**Further improvement**

**data/elders.yml**

```auto
- id: steve
  name: Steve Smith

- id: john
  name: John Smith

```

- $allElders - the Data file elders.yml array
- $elders - is a single element array of a map
- $elder - is the map of the desired elder element. Defining a variable at the top of the template provides access to the data within the template.
- .Params.elder\_id - page variable that contains the elder\_id for this chapter.

**Note** : this reason for the separate elder.yml file is that elder data is read from multiple pages and partial templates and I wanted to keep all elder data in a single file because the end-users will be using [Forestry.IO](http://Forestry.IO) to edit their site content and data files.

```auto
{{ define "main" }}
{{ $allElders := index .Site.Data.elders }}
{{ $elders := where $allElders "id" .Params.elder_id }}
{{ $elder := index $elders 0 }}

 <section class="container bg-containerBackground py-5 prose md:prose-md px-3">
     <p>{{ $elder.id }}</p>
 </section>
 {{ end }}

```

Hope this helps someone, I’m learning a lot today!

Karl

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [June 6, 2021, 10:07pm UTC](https://discourse.gohugo.io/t/how-to-select-array-element-by-element-property/33234/6 "2021-06-06T22:07:34Z")

</div>

Closed on June 6, 2021, 10:07pm UTC

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