Skip to main content
marcot0005
Inspiring
Feb 15, 2022 9:27 PM UTC
Question

Map with variable key

Is it somehow possible to create a map or dictionary using variables for the keys? For example, is there some way for me to do something like a!map(localvar1:local!var2) to make a user generated map? Or maybe use a foreach loop over a 2d array to convert it to a map? 

11 replies

csteward
Brainy
Feb 15, 2022 9:37 PM UTC

AFAIK there isn't any way to define the key within a!map() dynamically like this.  This sounds like a situation I would utilize the database to maintain dynamically defined key-value pairs, where the user can enter both and you can reference them where you need via a!queryEntity().

What is the use case?

marcot0005
Inspiring
Feb 15, 2022 9:42 PM UTC

I was wanting to use a!setGroupAttributes to make an interface that sets attributes on groups. The function requires a dictionary of attributes to set them and ideally I'd like the interface to allow one to set both the attribute names and values.

andrewh0007
Inspiring
Feb 15, 2022 10:00 PM UTC

Hi Marco,

The only way I've found of doing this is utilising JSON. Is below sort of what you're looking for?

a!localVariables(
  local!keys: {
    "keyOne",
    "keyTwo",
    "keyThree"
  },
  local!values: {
    117,
    "Appian",
    now()
  },
  local!keysAsJson: concat(
    "{",
    a!forEach(
      items: local!keys,
      expression: concat(
        char(34),
        fv!item,
        char(34),
        ":",
        char(34),
        char(34),
        if(
          fv!isLast,
          {},
          ","
        )
      )
    ),
    "}"
  ),
  local!keysAsDictionary: a!fromJson(local!keysAsJson),
  reduce(
    a!update,
    local!keysAsDictionary,
    merge(
      local!keys,
      local!values
    )
  )
)

marcot0005
Inspiring
Feb 15, 2022 10:05 PM UTC

That at least LOOKS like what I need. Gonna have to try it out to know for sure!

andrewh0007
Inspiring
Feb 15, 2022 10:09 PM UTC

Let me know how you go. But I would agree with Mike above. The solution I've proposed is only needed if the keys are unknown and attributes of groups is a known thing. Unless I'm misunderstanding your use case I don't believe my solution is required.

alexandru.boerescu
Employee
Sep 28, 2023 5:36 PM UTC

Hi, I know it's late, but I thought it'd be good to capture the response here.

You could use reduce with merge to achieve the desired outcome - not sure if all functions were available at the time you asked.

a!localVariables(
Â%A0 local!keys: { "keyOne", "keyTwo", "keyThree" },
Â%A0 local!values: { 117, "Appian", now() },
Â%A0 reduce(
Â%A0 Â%A0 a!update,
Â%A0 Â%A0 a!map(),
Â%A0 Â%A0 merge(local!keys, local!values)
Â%A0 )
)

Kudos to [mention:cef46f3735da42da9f3d7cb6d61d5347:e9ed411860ed4f2ba0265705b8793d05] for the coming up with the solution!

New Participant
Feb 8, 2024 2:51 AM UTC

Hi,

I know its late ... This also works ,

a!localVariables(
local!keys: { "keyOne", "keyTwo", "keyThree" },
local!values: { 117, "Appian", now() },

a!update(
data: a!map(),
index: local!keys,
value: local!values
)

)

output :

  • Map
      • keyOne117(Number (Integer))
          • keyTwo"Appian"(Text)
              • keyThree2/8/2024 2:46 AM GMT+00:00(Date and Time)