Map with variable key

Certified Senior Developer

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? 

  Discussion posts and replies are publicly visible

Parents
  • 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
        )
      )
    )

Reply
  • 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
        )
      )
    )

Children