Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flatten function to contain a list

I have the following variables:

companies = [
  {
    name        = "Company1"
    description = "Description for company 1"
    employees = [
      {
        name   = "Employee1"
      }
    ]
  },
  {
    name        = "Company2"
    description = "Description for company 2"
    employees = [
      {
        name   = "Employee2"
      },
      {
        name   = "Employee3"
      }
    ]
}]

This is the variable definition:

variable "companies" {
  type = list(object({
    name        = string,
    description = string,
    employees = list(object({
      name   = string,
      age    = string,
    }))
  }))
}

I have the following flatten function:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

locals {
  nestedlist = flatten([
    for company_key, company_value in var.companies : [
      for employee_key, employee_value in company_value.employees : {
        company_name        =   company_value.name
        company_description =   company_value.description
        employees           =   employee_value["name"]
      }
    ]
  ])
}

This produces:

flatten_output = [
      + {
          + company_description   = "Description for company 1"
          + company_name          = "Company1"
          + employees             = "Employee1"
        },
      + {
          + company_description   = "Description for company 2"
          + company_name          = "Company2"
          + employees             = "Employee2"
        },
      + {
          + company_description   = "Description for company 2"
          + company_name          = "Company2"
          + employees             = "Employee3"
        },
    ]

However I would want the following output:

flatten_output = [
      + {
          + company_description   = "Description for company 1"
          + company_name          = "Company1"
          + employees             = ["Employee1"]
        },
      + {
          + company_description   = "Description for company 2"
          + company_name          = "Company2"
          + employees             = ["Employee2","Employee3"]
        }
    ]

How can I ensure the employees in the output are a list containing all employee names?

>Solution :

We just need to restructure the nestedlist for expression to be a list(object), and to use a nested for expression to construct the list of employee names within a key in this object:

locals {
  nestedlist = [
    for company_key, company_value in local.companies : {
      company_name        = company_value.name
      company_description = company_value.description
      employees           = [for employee_key, employee_value in company_value.employees : employee_value["name"]]
    }
  ]
}

this produces:

output = [
  {
    company_description = "Description for company 1"
    company_name        = "Company1"
    employees           = [
      "Employee1",
    ]
  },
  {
    company_description = "Description for company 2"
    company_name        = "Company2"
    employees           = [
      "Employee2",
      "Employee3",
    ]
  },
]

as requested. I would probably also rename nestedlist since it is no longer a nested list.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading