Skip to main content
New Participant
Apr 27, 2023 7:22 PM UTC
Question

how to add item in list of maps

Hello,

i have a list of maps,

how do you add new map value?

and 

how do you update an index of repective value map?

this is the rule code (but it do not work):

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

a!localVariables(
local!campos: a!forEach(
items: ri!map,
expression: fv!item.sistema = ri!sistema
),

local!indice: where(local!campos,-1),

local!resultado:if(
local!indice > 0,
if(
a!isNullOrEmpty(ri!permiso),
remove(ri!map,local!indice),
/*update value in list*/
a!update(ri!map,local!indice,a!map(sistema: ri!sistema,permiso:ri!permiso))
),
/*add map to list*/
a!update(ri!map,length(ri!map)+1,a!map(sistema: ri!sistema,permiso:ri!permiso))
),
local!resultado
)

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    1 reply

    sanchitg0002
    Inspiring
    Apr 27, 2023 8:25 PM UTC

    Your logic for updating an index in a list of Map seems to be fine, for adding a map to list you can simply use append() function.

    Take reference from the sample code snippet.

    a!localVariables(
      local!map: {
        a!map(name: "Test", class: 10),
        a!map(name: "Test1", class: 10),
        a!map(name: "Test2", class: 10),
        a!map(name: "Test3", class: 10)
      },
      local!update: a!update(
        local!map,
        { 1, 2 },
        {
          a!map(name: "TEst100", class: 12),
          a!map(name: "TEst200", class: 11)
        }
      ),
      local!add: append(
        local!map,
        {
          a!map(name: "Test5", class: 12),
          a!map(name: "Test6", class: 12)
        }
      ),
      local!add
    )