Reputation: 1214
I have 3 arrays of objects. They look like this:
$arr1
, $arr2
: Objects with properties like (ID
, filename
, size
, etc).$arrlink
: Objects with properties like (file1ID
, file2ID
, misc). This links 2 of the above objectsWhat I need to do is combine them into one array so I can write it to a CSV. The 3 need to be linked: File1ID
and File2ID
to the ID
column in the other 2 arrays. I need to be able to pick what properties I want. Here's an example
$arr1[0] = {1,"c:\filename1.txt",1000....}
$arr1[1] = {2,"c:\filename2.txt",2000....}
$arrlink[0] = {1,2,"aaaa")
Desired Result:
newarray[0] = {"c:\filename1.txt", "c:\filename2.txt", "aaaa"}
I know I can foreach through and do this, but was wondering if there was a simpler/more direct way.
Of course, there are many items in each array, but they all link up in a 1 to 1 relationship.
Upvotes: 2
Views: 1056
Reputation: 1214
I wound up using this. It saves a lot of typing and doesn't need to be modified if I modify the underlying objects (as long as I leave the linked fields alone).
$arrOutput = @()
foreach($a in $arrMatch)
{
$objOut = New-Object PsObject
$a.psobject.properties | % { $objOut | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value}
$arrS1[$a.file1].psobject.properties | % { $objOut | Add-Member -MemberType $_.MemberType -Name ($_.Name + "_1") -Value $_.Value}
$arrS2[$a.file2].psobject.properties | % { $objOut | Add-Member -MemberType $_.MemberType -Name ($_.Name + "_2") -Value $_.Value}
$arrOutput+= $objOut
}
Upvotes: 1