0

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

1 Answer 1

2

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       
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! Is foreach necessary to accomplish this? I struggled hard to get it working using piping only.
If you want to do this in the pipeline,you might want to look at the -PipelineVariable parameter in V4. Don't know whether that will work with your function or not, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.