assemblyscript

How to iterate over dictionary in assemblyscript


let treasury: Record<string, string> = {};
treasury.firstWallet = "0xEA91B5E687a490380C52d264D5d36558d79F4188".toLowerCase()

for (let wallet in treasury) { ...

With this code I am getting the below, with a little red arrow between 'wallet' and 'in', and a '; expected' under 'wallet'

Compile subgraphERROR TS1110: Type expected.

   for (let wallet in treasury) {

So what's wrong with this and what's the proper way to do this?


Solution

  • AssemblyScript doesn't have dynamic objects. But you can use Map

    const treasury: Map<string, string> = new Map()
      .set("firstWallet", "0xea91b5e687a490380c52d264d5d36558d79f4188")
      .set("secondWallet", "0xea91b5e687a490380c52d264d5d36558d79f4188")
    
    const wallets = treasury.values();
    for (let i = 0, len = wallets.length; i < len; i++) {
      let wallet = wallets[i];
      // ...
    }