将对象数组拆分成行
Split up object array into rows
考虑一个returns对象列表X
的函数,其中X
有一个整数和一个数组对象。
Get-Foo
MyInteger MyStrings
--------- ---------
1 {A,B,C,D}
2 {A,B,C}
3 {A}
如何执行此列表的自连接以获得以下输出?
Get-Foo
MyInteger MyString
--------- --------
1 A
1 B
1 C
1 D
2 A
2 B
2 C
3 A
只需遍历每个对象上的 MyStrings,并为每个对象创建一个新对象:
function get-foo {
[PSCustomObject]@{MyInteger=1;MyStrings=@('A','B','C','D')}
[PSCustomObject]@{MyInteger=2;MyStrings=@('A','B','C')}
[PSCustomObject]@{MyInteger=3;MyStrings=@('A')}
}
get-foo |
foreach {
foreach ($string in $_.MyStrings)
{ [PSCustomObject]@{
MyInteger = $_.MyInteger
MyString=$string
}
}
} | ft -AutoSize
MyInteger MyString
--------- --------
1 A
1 B
1 C
1 D
2 A
2 B
2 C
3 A
考虑一个returns对象列表X
的函数,其中X
有一个整数和一个数组对象。
Get-Foo
MyInteger MyStrings
--------- ---------
1 {A,B,C,D}
2 {A,B,C}
3 {A}
如何执行此列表的自连接以获得以下输出?
Get-Foo
MyInteger MyString
--------- --------
1 A
1 B
1 C
1 D
2 A
2 B
2 C
3 A
只需遍历每个对象上的 MyStrings,并为每个对象创建一个新对象:
function get-foo {
[PSCustomObject]@{MyInteger=1;MyStrings=@('A','B','C','D')}
[PSCustomObject]@{MyInteger=2;MyStrings=@('A','B','C')}
[PSCustomObject]@{MyInteger=3;MyStrings=@('A')}
}
get-foo |
foreach {
foreach ($string in $_.MyStrings)
{ [PSCustomObject]@{
MyInteger = $_.MyInteger
MyString=$string
}
}
} | ft -AutoSize
MyInteger MyString
--------- --------
1 A
1 B
1 C
1 D
2 A
2 B
2 C
3 A