Skip to main content
May 9, 2024
Solved

list of dictionary in forEach

  • May 9, 2024
  • 5 replies
  • 0 views

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

    Best answer by mikes0011

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

    5 replies

    May 9, 2024

    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

    fyi:

    mikes0011
    mikes0011Answer
    Brainy
    May 9, 2024

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

    May 9, 2024

    Amazing! thank you so much