Transform two arrays into a map where the first array is the key and the second is the value

Certified Associate Developer

Hi,

I am parcticing with the a!queryRecordType() function and I realized in return one field called identifiers where the primiary key is returned and I would like to know if I could transform it together with the data field into a map where the identifiers values are the keys and the data values are the values.

For example:

Having:

identifiers : {12,20}

 data : {"first value", "second value"}

Transform it into

{12: "first value",

20: "second value}

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Senior Developer

    If you have the Dictionary Manipulation plugin:

    a!localVariables(
      local!identifiers:{12,20},
      local!data:{"first value","second value"},
      a!flatten(
        a!forEach(
          items: local!identifiers,
          expression: cast(typeof({a!map()}),createdictionary(fv!item,local!data[fv!index]))
        )
      )
    )

    That still gets you a list of maps, though. If you want them in a single text, you can joinarray(a!flatten...,",")

Reply
  • 0
    Certified Senior Developer

    If you have the Dictionary Manipulation plugin:

    a!localVariables(
      local!identifiers:{12,20},
      local!data:{"first value","second value"},
      a!flatten(
        a!forEach(
          items: local!identifiers,
          expression: cast(typeof({a!map()}),createdictionary(fv!item,local!data[fv!index]))
        )
      )
    )

    That still gets you a list of maps, though. If you want them in a single text, you can joinarray(a!flatten...,",")

Children
  • 0
    Certified Lead Developer
    in reply to robk

    Here's a way to do it that doesn't require the Dictionary Manipulation plugin. Note that the keys cannot key integers, they need to be Strings (hence the cast).

    a!localVariables(
      local!keys: { 12, 20 },
      local!values: { "first value", "second value" },
      local!keyValues: a!forEach(
        items: local!keys,
        expression: a!map(
          key: fv!item,
          value: index(local!values, fv!index, null)
        )
      ),
      a!update(
        a!map(),
        cast(typeof({ "" }), local!keyValues.key),
        local!keyValues.value
      )
    )