# Use object in for loop

**URL:** <https://talk.jekyllrb.com/t/use-object-in-for-loop/5879>\
**Category:** Help\
**Created:** [April 14, 2021, 12:42am UTC](https://talk.jekyllrb.com/t/use-object-in-for-loop/5879 "2021-04-14T00:42:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![symark](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@symark](https://talk.jekyllrb.com/u/symark)\
**Post date:** [April 14, 2021, 12:42am UTC](https://talk.jekyllrb.com/t/use-object-in-for-loop/5879/1 "2021-04-14T00:42:53Z")

</div>

I’m using the same layout for pages in different categories and wondering if there is a way to call object page.categories from the front matter in for loop.

{% for page in site.collection limit:3 %}  
{% if page.categories contains ‘{{ page.categories }}’ %}

### {{ page.title }}
  
{% endif %}  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [April 14, 2021, 6:45am UTC](https://talk.jekyllrb.com/t/use-object-in-for-loop/5879/2 "2021-04-14T06:45:43Z")

</div>

Please indent your code with 4 spaces before it or use backticks ``` before and after so it is monospaced.

````
```
{% for ... %}
{% endfor %}
```

````

e.g.

```auto
{% for ... %}
{% endfor %}

```

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [April 14, 2021, 6:48am UTC](https://talk.jekyllrb.com/t/use-object-in-for-loop/5879/3 "2021-04-14T06:48:41Z")

</div>

Your first issue is that your variables are colliding as names.

Page is already a variable.

So use `p` or `collection_page` or `item`.

```auto
{{ page.categories | inspect }} Categories of current page 

{% for p in site.collections %}
  {{ p.categories | inspect }} categories of current item 
  {% if p == page %}
...

```

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [April 14, 2021, 6:55am UTC](https://talk.jekyllrb.com/t/use-object-in-for-loop/5879/4 "2021-04-14T06:55:39Z")

</div>

## Aim

What are you trying to achieve? In terms of the frontend result of displaying collections and categories? You might be able to do something similar using `site.categories`.

## Code

Your comparison of categories seems weird with the quotes as that will not evaluate.

Plus it’s bad a idea to check if an array contains another array or hash. Try displaying an object to understand it before you use it.

````
```json
{{ p.categories | jsonify }}
```

```json
{{ page.categories | jsonify }}
```

````

* * *

## Categories not in collections

Plus categories are not supported in collections, which I cover more on below.

Note that collections cannot have categories in the usual sense. Only posts can. Well, posts is also a collection, but other collections won’t have categories.

Yes you _can_ set categories on any page like a collection. But it will _not_ add to the grouping of all categories in site.categories.

Further, a post will convert this string implicitly in Jekyll to an array

```auto
categories: abc def
# to
categories:
  - abc
  - def

```

While any non-posts that have categories won’t get that benefit. You’ll have to be explicit with the array form.
