展平函数以包含列表
Flatten function to contain a list
我有以下变量:
companies = [
{
name = "Company1"
description = "Description for company 1"
employees = [
{
name = "Employee1"
}
]
},
{
name = "Company2"
description = "Description for company 2"
employees = [
{
name = "Employee2"
},
{
name = "Employee3"
}
]
}]
这是变量定义:
variable "companies" {
type = list(object({
name = string,
description = string,
employees = list(object({
name = string,
age = string,
}))
}))
}
我有以下展平功能:
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"]
}
]
])
}
这会产生:
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"
},
]
但是我想要以下输出:
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"]
}
]
如何确保输出中的 employees
是包含所有员工姓名的列表?
我们只需要将 nestedlist
for
表达式重组为 list(object)
,并使用嵌套的 for
表达式构造员工姓名列表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"]]
}
]
}
这会产生:
output = [
{
company_description = "Description for company 1"
company_name = "Company1"
employees = [
"Employee1",
]
},
{
company_description = "Description for company 2"
company_name = "Company2"
employees = [
"Employee2",
"Employee3",
]
},
]
根据要求。我可能还会重命名 nestedlist
,因为它不再是嵌套列表。
我有以下变量:
companies = [
{
name = "Company1"
description = "Description for company 1"
employees = [
{
name = "Employee1"
}
]
},
{
name = "Company2"
description = "Description for company 2"
employees = [
{
name = "Employee2"
},
{
name = "Employee3"
}
]
}]
这是变量定义:
variable "companies" {
type = list(object({
name = string,
description = string,
employees = list(object({
name = string,
age = string,
}))
}))
}
我有以下展平功能:
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"]
}
]
])
}
这会产生:
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"
},
]
但是我想要以下输出:
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"]
}
]
如何确保输出中的 employees
是包含所有员工姓名的列表?
我们只需要将 nestedlist
for
表达式重组为 list(object)
,并使用嵌套的 for
表达式构造员工姓名列表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"]]
}
]
}
这会产生:
output = [
{
company_description = "Description for company 1"
company_name = "Company1"
employees = [
"Employee1",
]
},
{
company_description = "Description for company 2"
company_name = "Company2"
employees = [
"Employee2",
"Employee3",
]
},
]
根据要求。我可能还会重命名 nestedlist
,因为它不再是嵌套列表。