l33t
l33t

Reputation: 19937

Split up object array into rows

Consider a function that returns a list of object X where X has an integer and an array of objects.

Get-Foo

MyInteger   MyStrings
---------   ---------
        1   {A,B,C,D}
        2   {A,B,C}
        3   {A}

How do I perform a self-join of this list in order to get the output below?

Get-Foo

MyInteger   MyString
---------   --------
        1   A
        1   B
        1   C
        1   D
        2   A
        2   B
        2   C
        3   A

Upvotes: 0

Views: 641

Answers (1)

mjolinor
mjolinor

Reputation: 68273

Just iterate through MyStrings on each object, and create a new object for each one:

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       

Upvotes: 2

Related Questions