Skip to main content
Known Participant
May 9, 2024 12:38 PM UTC
Solved

list of dictionary in forEach

Hi Guys,

I would need to get one list of dictionaries and not two lists of dictionaries? how can I proceed?

I expect: this result 

Now my code is like this: how can I fix?

a!localVariables(
  local!data:{date(2023,10,10),date(2024,10,10)},
  local!country:{"ITALY","USA"},
  a!forEach(
    items: local!data,
    expression: 
   a!forEach(
    local!country,
    {
      DATA:local!data,
      COUNTRY:fv!item
    }
  ))
  
)

    mikes0011
    mikes0011Answer
    Brainy
    May 9, 2024 12:50 PM UTC

    Ganesh's answer above gets close but doesn't give the correct distribution of dates to countries (it just repeats the same two).

    Here's my solution, which stores a reference to the current item in the outer forEach loop for reference in the inner loop (otherwise it's difficult since the loop-specific scope variables in the inner loop refer ONLY to that loop).  We still of course need to call a!flatten() on the final generated list.

    a!localVariables(
      local!dates: {date(2023,10,10), date(2024,10,10)},
      local!country: {"ITALY", "USA"},
      
      local!firstList: a!forEach(
        items: local!dates,
        expression: a!localVariables(
          
          local!currentDate: fv!item, /* we need to manually store a reference to the current outer loop item when we try to access it from the inner loop */
        
          a!forEach(
            local!country,
            {
              DATE: local!currentDate,
              COUNTRY: fv!item
            }
          )
        )
      ),
      
      a!flatten(local!firstList)
    )

    The Expression Guru

    5 replies

    Known Participant
    May 9, 2024 12:45 PM UTC

    a!localVariables(
      local!data: { date(2023, 10, 10), date(2024, 10, 10) },
      local!country: { "ITALY", "USA" },
      a!flatten(
        a!forEach(
          items: local!data,
          expression: a!forEach(
            local!country,
            {
              DATA: local!data[fv!index],
              COUNTRY: fv!item
            }
          )
        )
      )
    )

    mikes0011
    Brainy
    May 9, 2024 12:53 PM UTC

    fyi:

    The Expression Guru
    mikes0011
    mikes0011Answer
    Brainy
    May 9, 2024 12:50 PM UTC

    Ganesh's answer above gets close but doesn't give the correct distribution of dates to countries (it just repeats the same two).

    Here's my solution, which stores a reference to the current item in the outer forEach loop for reference in the inner loop (otherwise it's difficult since the loop-specific scope variables in the inner loop refer ONLY to that loop).  We still of course need to call a!flatten() on the final generated list.

    a!localVariables(
      local!dates: {date(2023,10,10), date(2024,10,10)},
      local!country: {"ITALY", "USA"},
      
      local!firstList: a!forEach(
        items: local!dates,
        expression: a!localVariables(
          
          local!currentDate: fv!item, /* we need to manually store a reference to the current outer loop item when we try to access it from the inner loop */
        
          a!forEach(
            local!country,
            {
              DATE: local!currentDate,
              COUNTRY: fv!item
            }
          )
        )
      ),
      
      a!flatten(local!firstList)
    )

    The Expression Guru
    Known Participant
    May 9, 2024 1:04 PM UTC

    Amazing! thank you so much