Skip to main content
New Participant
February 17, 2021
Question

iterate the values and assign to user

  • February 17, 2021
  • 1 reply
  • 0 views

Im trying to write a expression rule where there are two ri variables , one ri variable has type list of users and other ri variable is of type list of numbers.

something like below

ri!users: {"abc","def"}

ri!numbers: {1,2,3,4,5,6,7}

i have a use case where i should need to receive list of dictionary in below format

{

user:"abc",

number:1

},

{

user:"def",

number:2

},

{

user:"abc",

number:3

},

{

user:"def",

number:4

},

{

user:"abc",

number:5

},

{

user:"def",

number:6

}

.

.

. etc

1 reply

Participating Frequently
February 17, 2021

This needs further work (extracting common rules, validations, etc.), but it should give you a good starting point:

= a!localVariables(
  local!userCount: count(ri!users),
  local!numberCount: count(ri!numbers),
  local!resultCount: max(local!userCount, local!numberCount),
  
  a!forEach(
    items: 1+enumerate(local!resultCount),
    expression: a!localVariables(
      local!userIdx: mod(fv!index, local!userCount),
      local!numberIdx: mod(fv!index, local!numberCount),
      {
        user: ri!users[if(local!userIdx=0, local!userCount, local!userIdx)],
        number: ri!numbers[if(local!numberIdx=0, local!numberCount, local!numberIdx)]
      }
    )
  )
)