Skip to main content
Participating Frequently
Jan 9, 2023 8:02 AM UTC
Solved

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

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}

konduruc733182
Inspiring
Jan 9, 2023 8:13 AM UTC

Hello antonioc1541,

Please refer the below code. I hope this will help.

a!localVariables(
  local!identifiers: { 12, 20 },
  local!data: { "first value", "second value" },
  {
    append(
      a!forEach(
        items: local!identifiers,
        expression: concat(
          fv!item,
          ": ",
          index(local!data, fv!index, null)
        )
      )
    )
  }
)

can you also try this,

a!localVariables(
  local!identifiers: { 12, 20 },
  local!data: { "first value", "second value" },
  {

     a!flatten(
       a!forEach(
         items: local!identifiers,
         expression: {
           a!map(
             identifier:fv!item,
             data:index(local!data,fv!index,null)
           )
         }
       )
     )
   
  }
)

7 replies

konduruc733182
Inspiring
Jan 9, 2023 8:13 AM UTC

Hello antonioc1541,

Please refer the below code. I hope this will help.

a!localVariables(
  local!identifiers: { 12, 20 },
  local!data: { "first value", "second value" },
  {
    append(
      a!forEach(
        items: local!identifiers,
        expression: concat(
          fv!item,
          ": ",
          index(local!data, fv!index, null)
        )
      )
    )
  }
)

can you also try this,

a!localVariables(
  local!identifiers: { 12, 20 },
  local!data: { "first value", "second value" },
  {

     a!flatten(
       a!forEach(
         items: local!identifiers,
         expression: {
           a!map(
             identifier:fv!item,
             data:index(local!data,fv!index,null)
           )
         }
       )
     )
   
  }
)

Participating Frequently
Jan 9, 2023 8:30 AM UTC

Hi,

thank you for your answer, it´s almost what I want but it returns a List of Map and I would like a single Map containing all the data is there any posibilitiy to join them.

Thanks again.

Participating Frequently
Jan 9, 2023 8:31 AM UTC

It is possible to do this but it's a rather non-standard scenario. What are you trying to achieve by creating this map?

Participating Frequently
Jan 9, 2023 8:39 AM UTC

I'm making a process model where I get an ID and I have to look up in the result of an a!queryRecordType() that I had already saved in an array and I thought the fastest way to do it was to make a map instead of an array where the key were the ID I'm looking for.

Participating Frequently
Jan 9, 2023 8:52 AM UTC

In this way you would duplicate the data you already have into another process variable. That is not memory efficient.

Alternative to your solution you could:

- Create an expression that returns the requested value based on rule inpuds: 1/ given id and  2/ data array (originating from your a!queryRecordType()).

- Just query single item on given ID. Create seperate expression rule and set as script task data output in your PM.

Both produce re-usable components that make sense when building an Appian application.

New Participant
Jun 30, 2023 6:14 PM UTC

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...,",")

mathieud0001
Genius
Jun 30, 2023 7:04 PM UTC

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