# How do I convert a Trie.Trie to an Array in Motoko?

**URL:** <https://forum.dfinity.org/t/how-do-i-convert-a-trie-trie-to-an-array-in-motoko/8120>\
**Category:** Developers\
**Created:** [October 24, 2021, 11:11am UTC](https://forum.dfinity.org/t/how-do-i-convert-a-trie-trie-to-an-array-in-motoko/8120 "2021-10-24T11:11:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [October 24, 2021, 11:11am UTC](https://forum.dfinity.org/t/how-do-i-convert-a-trie-trie-to-an-array-in-motoko/8120/1 "2021-10-24T11:11:28Z")

</div>

I’m attempting to convert a Trie.Trie into an array. Go someone give me an example of how this is done. Thanks in advance!

---

<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:** [October 24, 2021, 3:07pm UTC](https://forum.dfinity.org/t/how-do-i-convert-a-trie-trie-to-an-array-in-motoko/8120/2 "2021-10-24T15:07:32Z")

</div>

You could create an iterator using Trie.iter() and use Iter.toArray(), eg:

> import Trie “mo:base/Trie”;  
> import Text “mo:base/Text”;  
> import Iter “mo:base/Iter”;
> 
> type Trie\<K, V\> = Trie.Trie\<K, V\>;  
> type Key = Trie.Key;
> 
> func key(t: Text) : Key { { key = t; hash = Text.hash(t) } };
> 
> let t0 : Trie\<Text, Nat\> = Trie.empty();  
> let t1 : Trie\<Text, Nat\> = Trie.put(t0, key(“one”), Text.equal, 1).0;  
> let t2 : Trie\<Text, Nat\> = Trie.put(t1, key(“two”), Text.equal, 2).0;
> 
> let my\_array : [(Text, Nat)] = Iter.toArray(Trie.iter(t2));

This will give you an array of key/value pairs like so: [(“one”, 1), (“two”, 2)]

Or you could use Trie.toArray(), which takes a function that maps from key/value pairs to a third type (which could still be a key/value pair or for example just the value), see here:  
[https://github.com/dfinity/motoko-base/blob/890b56688ed47c45ca3baf68bfa5c5ea22b7de88/src/Trie.mo#L962](https://github.com/dfinity/motoko-base/blob/890b56688ed47c45ca3baf68bfa5c5ea22b7de88/src/Trie.mo#L962)

---

<div class="post-metadata">

**Author:** ![tobyjaguar](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/tobyjaguar/32/4434_2.png) [@tobyjaguar](https://forum.dfinity.org/u/tobyjaguar)\
**Post date:** [July 22, 2022, 11:44pm UTC](https://forum.dfinity.org/t/how-do-i-convert-a-trie-trie-to-an-array-in-motoko/8120/3 "2022-07-22T23:44:26Z")

</div>

> [@Ori](#):
>
> [motoko-base/src/Trie.mo at 890b56688ed47c45ca3baf68bfa5c5ea22b7de88 · dfinity/motoko-base · GitHub](https://github.com/dfinity/motoko-base/blob/890b56688ed47c45ca3baf68bfa5c5ea22b7de88/src/Trie.mo#L962)

Is there an example implementation of using the toArray function of the Trie? I am looking at the toArray source from the link, and the application is not jumping out at me…

Also, I am curious which is a more optimized implementation, using toArray on the Trie or using the iterator?

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [July 23, 2022, 11:47am UTC](https://forum.dfinity.org/t/how-do-i-convert-a-trie-trie-to-an-array-in-motoko/8120/4 "2022-07-23T11:47:55Z")

</div>

> [@tobyjaguar](#):
>
> Is there an example implementation of using the toArray function of the Trie? I am looking at the toArray source from the link, and the application is not jumping out at me…

```auto
let myTrie
  : Trie.Trie<Text, Nat>
  = Trie.empty();

let myArray
  : [(Text, Nat)]
  = Trie.toArray<Text, Nat, (Text, Nat)>(myTrie, func (k, v) {
    (k, v);
  });

```

* * *

> [@tobyjaguar](#):
>
> Also, I am curious which is a more optimized implementation, using toArray on the Trie or using the iterator?

`Trie.toArray` seems optimal to me.

---

<div class="post-metadata">

**Author:** ![tobyjaguar](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/tobyjaguar/32/4434_2.png) [@tobyjaguar](https://forum.dfinity.org/u/tobyjaguar)\
**Post date:** [July 27, 2022, 5:45pm UTC](https://forum.dfinity.org/t/how-do-i-convert-a-trie-trie-to-an-array-in-motoko/8120/5 "2022-07-27T17:45:57Z")

</div>

great! thank you…got this working 🤩

for posterity this is what I ended up doing:

```auto
public shared func getRecords() : async [Record] {
  let result : [Record] = 
    Trie.toArray<Text, Record, Record>(records, func(k, v)) {
      (v);
    });
  return results;
}

```
