iterate over a map key

Certified Lead Developer

Hi Champs,

I have a map value having multiple key and value pair. Now I want to check for a value which exists in one or more key. So I guess the only way to do that is to iterate over the keys in the map. 

How can I achieve the same. As of my knowledge, for each works for no of map not for no of key/value pair in a map.

Regards,

Ghanashyam

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    If you define an array of your key names you can iterate over them with a nested a!forEach(). Example below, you can do what you want with the test and return value on the inner loop.

    a!localVariables(
      local!map: {
        a!map(
          label: "Map 1",
          key1: "A",
          key2: "C",
          key3: "C"
        ),
        a!map(
          label: "Map 2",
          key1: "C",
          key2: "A",
          key3: "B"
        ),
        a!map(
          label: "Map 3",
          key1: "C",
          key2: "B",
          key3: "C"
        )
      },
      local!definedKeys: { "key1", "key2", "key3" },
      a!forEach(
        items: local!map,
        expression: a!localVariables(
          local!innerMap: fv!item,
          a!forEach(
            items: local!definedKeys,
            expression: if(
              local!innerMap[fv!item] ="C",
              fv!item,
              {}
            )
          )
        )
      )
    )

  • 0
    Certified Lead Developer

    If you always have the same keys...

    local!key1: index(local!map, "key1"),
    local!key2: index(local!map, "key2"),
    local!key3: index(local!map, "key3"),

    local!int1: union(intersection(local!key1, local!key2),intersection(local!key1, local!key2)),
    local!int2: union(intersection(local!key1, local!key3),intersection(local!key1, local!key3)),
    local!int3: union(intersection(local!key2, local!key3),intersection(local!key2, local!key3)),

    local!int1 & "//" & local!int2 & "//" & local!int3

  • 0
    Certified Lead Developer

    No. As variables in Appian are immutable, you cannot update anything during iterations. But using some of the set functions, you can easily do this.

  • 0
    Certified Lead Developer

    remember that a!keys() grabs all keys (as a text array) in a particular dictionary, which you can grab into a local variable and then iterate over.  As Stefan pointed out, you can't push new values into the existing map, but if (as your post implies) you only need to check which keys contain a certain value, then this should work fine.  It should work similarly to Tim's suggested code except you wouldn't need to hardcode your key values in the "local!definedKeys" variable, you'd just use a!keys() there pointed to your map variable.