# How to traverse inner array of a trie?

**URL:** <https://forum.dfinity.org/t/how-to-traverse-inner-array-of-a-trie/12941>\
**Category:** Developers\
**Tags:** Functional-Programming, candid, Discussing\
**Created:** [May 13, 2022, 7:11am UTC](https://forum.dfinity.org/t/how-to-traverse-inner-array-of-a-trie/12941 "2022-05-13T07:11:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![manubodhi](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/manubodhi/32/3485_2.png) [@manubodhi](https://forum.dfinity.org/u/manubodhi)\
**Post date:** [May 13, 2022, 7:11am UTC](https://forum.dfinity.org/t/how-to-traverse-inner-array-of-a-trie/12941/1 "2022-05-13T07:11:21Z")

</div>

Hi,  
In my case i have a function/API in my code.  
Where i query a triemap and getting subset of the triemap.  
This given function is a working one and i want to change it. here i am comparing the parameter dishTypeId to v.dishTypeId, this works well

```auto
public query func getDishesByDishTypeId (dishTypeId : Types.DishTypeId) : async [(Types.DishId, Types.Dish)] {
       let trieOfDishes = Trie.filter<Types.DishId, Types.Dish>(dishes, func (k, v) { v.dishTypeId == dishTypeId } );

       let arrayOfDishes : [(Types.DishId, Types.Dish)] = Iter.toArray(Trie.iter(trieOfDishes));

       return arrayOfDishes;
    };

```

here “v” represents one dish from dishes triemap

dish is having the below candid definition

```auto
public type Dish = {
        dishId : DishId;
        images : [Image];
        defaultImage : Image;
        name : Text;
        defaultName : Text;
        ingredientIds : [IngredientId];
        description : Text;
        mealTypeIds : [MealTypeId];
        cuisineId : CuisineId;
        dishTypeId : DishTypeId;
        alternativeNames : [AlternativeName];
        draft : Bool;
    };

```

but i have to check mealTypeId in the next function, so i have to somehow check whether the mealTypeIds inside dish (here “v”) contains the passed mealTypeId how to do this? any idea? i am stuck on this for past 1 week, i am not getting a clue from any of the examples and even from cancan project? any help would be appreciated. Thanks in advance

NB: i am expecting something similar to this but i can’t find any useful functions like contains from the docs

```auto
public query func getDishesByDishTypeId (mealTypeId : Types.MealTypeId) : async [(Types.DishId, Types.Dish)] {
       let trieOfDishes = Trie.filter<Types.DishId, Types.Dish>(dishes, func (k, v) { 

        v.mealTypeIds.contains(mealTypeId) //how to do this in motoko?!

 } );

       let arrayOfDishes : [(Types.DishId, Types.Dish)] = Iter.toArray(Trie.iter(trieOfDishes));

       return arrayOfDishes;
    };

```

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [May 13, 2022, 7:51am UTC](https://forum.dfinity.org/t/how-to-traverse-inner-array-of-a-trie/12941/2 "2022-05-13T07:51:51Z")

</div>

With what’s currently in the base library, something like

```auto
Array.find(v.mealTypeIds, func(x : MealTypeId) { x == mealTypeId }) != null

```

would do the trick.

---

<div class="post-metadata">

**Author:** ![manubodhi](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/manubodhi/32/3485_2.png) [@manubodhi](https://forum.dfinity.org/u/manubodhi)\
**Post date:** [May 13, 2022, 8:05am UTC](https://forum.dfinity.org/t/how-to-traverse-inner-array-of-a-trie/12941/3 "2022-05-13T08:05:48Z")

</div>

> [@rossberg](#):
>
> `Array.find(v.mealTypeIds, func(x : MealTypeId) { x == mealTypeId }) != null`

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/a/a50bf0ed304a42a432f7f63e9548252e53e95c5a.png)

This is what it is showing now should i type cast this or something? what am i doing wrong here?

---

<div class="post-metadata">

**Author:** ![quint](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/quint/32/40449_2.png) [@quint](https://forum.dfinity.org/u/quint)\
**Post date:** [May 13, 2022, 9:38am UTC](https://forum.dfinity.org/t/how-to-traverse-inner-array-of-a-trie/12941/4 "2022-05-13T09:38:15Z")

</div>

The anonymous functions was missing a return type:

```auto
Array.find(v.mealTypeIds, func(x : MealTypeId) : Bool { x == mealTypeId }) != null

```

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [May 13, 2022, 9:53am UTC](https://forum.dfinity.org/t/how-to-traverse-inner-array-of-a-trie/12941/5 "2022-05-13T09:53:20Z")

</div>

Sorry, yes, what @quint said. Alternatively, you can explicitly instantiate the generic type of `find`, then the type of the anonymous function can be inferred from that:

```auto
Array.find<MealTypeId>(v.mealTypeIds, func(x) { x == mealTypeId }) != null

```

---

<div class="post-metadata">

**Author:** ![manubodhi](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/manubodhi/32/3485_2.png) [@manubodhi](https://forum.dfinity.org/u/manubodhi)\
**Post date:** [May 13, 2022, 10:22am UTC](https://forum.dfinity.org/t/how-to-traverse-inner-array-of-a-trie/12941/6 "2022-05-13T10:22:38Z")

</div>

Thank you very much @rossberg@quint you guys are great. I am struggling to learn all these, where i can actually get an intermediate level of project to learn or any tutorial to start with?! any idea? Thank you very much for the help once again \<3

---

<div class="post-metadata">

**Author:** ![Ori](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@Ori](https://forum.dfinity.org/u/Ori)\
**Post date:** [May 14, 2022, 4:23pm UTC](https://forum.dfinity.org/t/how-to-traverse-inner-array-of-a-trie/12941/7 "2022-05-14T16:23:23Z")

</div>

You can find some great content to dive into in the examples repo, if it helps: [https://github.com/dfinity/examples/tree/master/motoko](https://github.com/dfinity/examples/tree/master/motoko)

And the base library’s test dir can be useful too: [https://github.com/dfinity/motoko-base/tree/master/test](https://github.com/dfinity/motoko-base/tree/master/test)

There’s also a whole lot more on the hackathon developer resources page: [https://support.dfinity.org/hc/en-us/articles/4965948618772-Supernova-Hackathon-2022-Developer-Resources](https://support.dfinity.org/hc/en-us/articles/4965948618772-Supernova-Hackathon-2022-Developer-Resources)
