Converting the datasubset

Hi, I have a datasubset:

{{impact: 0.0, country_nm: "AA", year: "2019"},

{impact: 0.0, country_nm: "AA", year: "2020"},

{impact: 0.0, country_nm: "AA", year: "2021"},

{impact: 0.0, country_nm: "AA", year: "2022"},

{impact: 0.0, country_nm: "AA", year: "2023"},

{impact: 0.0, country_nm: "BB", year: "2019"},

{impact: 0.0, country_nm: "BB", year: "2020"},

{impact: 0.0, country_nm: "BB", year: "2021"},

{impact: 0.0, country_nm: "BB", year: "2022"},

{impact: 0.0, country_nm: "BB", year: "2023"}

},

And what I am trying to do is to convert it to different datasabsuet:

{

{country_nm: "AA",{

{year: "2019", impact:0},

{year: "2020", impact:0},

{year: "2021", impact:0},

{year: "2022", impact:0},

{year: "2023", impact:0}

}},

{country_nm: "BB",{

{year: "2019", impact:0},

{year: "2020", impact:0},

{year: "2021", impact:0},

{year: "2022", impact:0},

{year: "2023", impact:0}

}}

}

Is it possible to do?

Thanks

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    FWIW, a!forEach is unexpectedly powerful, and can be nested pretty easily.  This is what I was able to whip up quickly:

    a!localVariables(
      
      local!originalData: {
        {impact: 0.0, country_nm: "AA", year: "2019"},
        {impact: 0.0, country_nm: "AA", year: "2020"},
        {impact: 0.0, country_nm: "AA", year: "2021"},
        {impact: 0.0, country_nm: "AA", year: "2022"},
        {impact: 0.0, country_nm: "AA", year: "2023"},
        {impact: 0.0, country_nm: "BB", year: "2019"},
        {impact: 0.0, country_nm: "BB", year: "2020"},
        {impact: 0.0, country_nm: "BB", year: "2021"},
        {impact: 0.0, country_nm: "BB", year: "2022"},
        {impact: 0.0, country_nm: "BB", year: "2023"}
      },
      
      local!uniqueCountries: union(local!originalData.country_nm, local!originalData.country_nm),
      
      a!forEach(
        local!uniqueCountries,
        a!localVariables(
          local!currentCountry: fv!item,
          {
            country_nm: fv!item,
            country_data: a!forEach(
              local!originalData,
              if(
                fv!item.country_nm = local!currentCountry,
                {
                  year: fv!item.year,
                  impact: fv!item.impact
                },
                {}
              )
            )
          }
        )
      )
    )

    it seems to do what you want, though in the transformed data set it's necessary to label the "year data" arrays i believe.

Reply
  • 0
    Certified Lead Developer

    FWIW, a!forEach is unexpectedly powerful, and can be nested pretty easily.  This is what I was able to whip up quickly:

    a!localVariables(
      
      local!originalData: {
        {impact: 0.0, country_nm: "AA", year: "2019"},
        {impact: 0.0, country_nm: "AA", year: "2020"},
        {impact: 0.0, country_nm: "AA", year: "2021"},
        {impact: 0.0, country_nm: "AA", year: "2022"},
        {impact: 0.0, country_nm: "AA", year: "2023"},
        {impact: 0.0, country_nm: "BB", year: "2019"},
        {impact: 0.0, country_nm: "BB", year: "2020"},
        {impact: 0.0, country_nm: "BB", year: "2021"},
        {impact: 0.0, country_nm: "BB", year: "2022"},
        {impact: 0.0, country_nm: "BB", year: "2023"}
      },
      
      local!uniqueCountries: union(local!originalData.country_nm, local!originalData.country_nm),
      
      a!forEach(
        local!uniqueCountries,
        a!localVariables(
          local!currentCountry: fv!item,
          {
            country_nm: fv!item,
            country_data: a!forEach(
              local!originalData,
              if(
                fv!item.country_nm = local!currentCountry,
                {
                  year: fv!item.year,
                  impact: fv!item.impact
                },
                {}
              )
            )
          }
        )
      )
    )

    it seems to do what you want, though in the transformed data set it's necessary to label the "year data" arrays i believe.

Children
No Data