# HubDB selectrattr variable

**URL:** <https://community.hubspot.com/t/hubdb-selectrattr-variable/59245>\
**Category:** CMS Development\
**Tags:** hubdb, local-development\
**Created:** [June 17, 2022, 7:37am UTC](https://community.hubspot.com/t/hubdb-selectrattr-variable/59245 "2022-06-17T07:37:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanntn](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sanntn/32/99782_2.png) [@sanntn](https://community.hubspot.com/u/sanntn)\
**Post date:** [June 17, 2022, 7:37am UTC](https://community.hubspot.com/t/hubdb-selectrattr-variable/59245/1 "2022-06-17T07:37:38Z")

</div>

Hi,

I was wondering if it was possible to use a variable when filtering Hubdb data so I wouldn’t have to create multiple row queries.  
Here’s my setup.

{% set data = hubdb\_table\_rows(xxxxxx) %}  
.

{% for country in countries %}

{% set overview = data|selectattr(‘country’,‘equalto’, country) %}  
.

{% endfor %}

{% endfor %}

So, is there some kind of a trick to use variables in this situation. Thanks!

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [June 17, 2022, 5:30pm UTC](https://community.hubspot.com/t/hubdb-selectrattr-variable/59245/2 "2022-06-17T17:30:50Z")

</div>

Hi @sanntn 👋 Thanks for reaching out. Let’s see if we can get this conversation started — @alyssamwilie @Kevin-C @LMeert do you have any experience or insight here?

Thank you! — Jaycee

---

<div class="post-metadata">

**Author:** ![Kevin-C](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin-c/32/34570_2.png) [@Kevin-C](https://community.hubspot.com/u/Kevin-C)\
**Post date:** [June 17, 2022, 10:11pm UTC](https://community.hubspot.com/t/hubdb-selectrattr-variable/59245/3 "2022-06-17T22:11:57Z")

</div>

Hey @sanntn

Thanks [@Jaycee\_Lewis](https://community.hubspot.com/t5/user/viewprofilepage/user-id/127074)

You’ll want to review this [documentation](https://developers.hubspot.com/docs/cms/data/hubdb) and [the operators docs](https://developers.hubspot.com/docs/cms/hubl/operators-and-expression-tests), but the implementation is rather simple:

```lia-code-sample
{% set country = "Armenia" %}
{% set country_query = "name__eq=" ~ country %}
{% set data = hubdb_table_rows(5424505) %}
{% set select_data = data|selectattr("name","eq",country) %}
{{ country }}<hr>
{% for item in select_data %}
 {{ item.name }} - {{ item.gdp }}<br>
{% endfor %}

```

or

```lia-code-sample
{% set country = "Armenia" %}
{% set country_query = "name__eq=" ~ country %}
{% set data = hubdb_table_rows(5424505, country_query) %}
{{ country }}<hr>
{% for item in data %}
 {{ item.name }} - {{ item.gdp }}<br>
{% endfor %}

```

The output:

![KevinC_0-1655503858312.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/1/154943b95d0464bae267bedd265502eaac2a4413.png)

Hope this gets you moving!

---

<div class="post-metadata">

**Author:** ![PDeT](https://avatars.discourse-cdn.com/v4/letter/p/f08c70/32.png) [@PDeT](https://community.hubspot.com/u/PDeT)\
**Post date:** [November 22, 2022, 9:58pm UTC](https://community.hubspot.com/t/hubdb-selectrattr-variable/59245/4 "2022-11-22T21:58:47Z")

</div>

I’m trying ti figure out why this solution is not working for me.

If i use a value definition for country, like in the example, with a defined string as value, it works. However, when I assign the value for the country variable from another variable (a contact property value in my case) it fails and the selectattr returns zero results.

so

{% set country=“Armenia” %} works

but

{% set country= contact[‘country’] %}

will not result in a match, however, echoing both {{ country}} and {{contact[‘country’] }} shows me Armenia twice.

any ideas why this happens?

---

<div class="post-metadata">

**Author:** ![Kevin-C](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin-c/32/34570_2.png) [@Kevin-C](https://community.hubspot.com/u/Kevin-C)\
**Post date:** [November 23, 2022, 2:10am UTC](https://community.hubspot.com/t/hubdb-selectrattr-variable/59245/5 "2022-11-23T02:10:58Z")

</div>

Hey @PDeT

Comparing strings can be tricky! Look into using a few filters to clean up the strings comparing. Potential [filters](https://developers.hubspot.com/docs/cms/hubl/filters) being; trim, lower, replace, and regex\_replace.

Edit:

I would also check that the user is cookied before banging your head against the wall too many times!

---

<div class="post-metadata">

**Author:** ![PDeT](https://avatars.discourse-cdn.com/v4/letter/p/f08c70/32.png) [@PDeT](https://community.hubspot.com/u/PDeT)\
**Post date:** [November 23, 2022, 9:41am UTC](https://community.hubspot.com/t/hubdb-selectrattr-variable/59245/6 "2022-11-23T09:41:28Z")

</div>

Hi Kevin, thx for tuning in!

Even if i set the value to match to equal the table row value, it does not find a match:

{% set rows = hubdb\_table\_rows(XXXX) %}

{% set test = hubdb\_table\_row(XXXX,YYYY) %}  
{% set queryValue = test.title %}

{% set match = rows|selectattr(‘title’,‘eq’, queryValue ) %}

{{ match }}

renders: []

if test.title is not going to match with any row title field from the table it was taken, then there is something really wrong with selectattr. My guess is that there is something wrong with the parser, when the variable value is not known at compile time.

---

<div class="post-metadata">

**Author:** ![PDeT](https://avatars.discourse-cdn.com/v4/letter/p/f08c70/32.png) [@PDeT](https://community.hubspot.com/u/PDeT)\
**Post date:** [November 28, 2022, 10:21am UTC](https://community.hubspot.com/t/hubdb-selectrattr-variable/59245/7 "2022-11-28T10:21:47Z")

</div>

forgot to mention @Kevin-C
