How can I merge two arrays object into one array object in PowerShell?
Array A
| Date | AAA |
| 2023-01-01 | 1 |
| 2023-01-02 | 2 |
| 2023-01-03 | 3 |
| 2023-01-04 | 4 |
| 2023-01-05 | 5 |
| 2023-01-06 | 6 |
| 2023-01-07 | 7 |
| 2023-01-08 | 8 |
| 2023-01-09 | 9 |
| 2023-01-10 | 10 |
Array B
| D_Date | BBB |
| 2023-01-06 | 6 |
| 2023-01-07 | 7 |
| 2023-01-08 | 8 |
| 2023-01-09 | 9 |
| 2023-01-10 | 10 |
Result
| Date | AAA | BBB |
| 2023-01-01 | 1 | |
| 2023-01-02 | 2 | |
| 2023-01-03 | 3 | |
| 2023-01-04 | 4 | |
| 2023-01-05 | 5 | |
| 2023-01-06 | 6 | 6 |
| 2023-01-07 | 7 | 7 |
| 2023-01-08 | 8 | 8 |
| 2023-01-09 | 9 | 9 |
| 2023-01-10 | 10 | 10 |
Here is my code example.
$listA = [pscustomobject]@(
[pscustomobject]@{
Date = Get-Date "2023-01-01"
AAA = "1"
}, [pscustomobject]@{
Date = Get-Date "2023-01-02"
AAA = "2"
}, [pscustomobject]@{
Date = Get-Date "2023-01-03"
AAA = "3"
}, [pscustomobject]@{
Date = Get-Date "2023-01-04"
AAA = "4"
}, [pscustomobject]@{
Date = Get-Date "2023-01-05"
AAA = "5"
}, [pscustomobject]@{
Date = Get-Date "2023-01-06"
AAA = "6"
}, [pscustomobject]@{
Date = Get-Date "2023-01-07"
AAA = "7"
}, [pscustomobject]@{
Date = Get-Date "2023-01-08"
AAA = "8"
}, [pscustomobject]@{
Date = Get-Date "2023-01-09"
AAA = "9"
}, [pscustomobject]@{
Date = Get-Date "2023-01-10"
AAA = "10"
}
)
$listB = [pscustomobject]@(
[pscustomobject]@{
D_Date = Get-Date "2023-01-06"
BBB = "6"
}, [pscustomobject]@{
D_Date = Get-Date "2023-01-07"
BBB = "7"
}, [pscustomobject]@{
D_Date = Get-Date "2023-01-08"
BBB = "8"
}, [pscustomobject]@{
D_Date = Get-Date "2023-01-09"
BBB = "9"
}, [pscustomobject]@{
D_Date = Get-Date "2023-01-10"
BBB = "10"
}
)
foreach ($objA in $listA) {
$objB = $listB | ? { $_.D_Date -eq $objA.Date }
$objA | Add-Member -Name "BBB" -Type NoteProperty -Value $objB.BBB
}
I try to loop list A and assign property BBB with a value from list B but it seems very slow for the large array (4000-5000 objects) and in case of list B result is more than list A my code will not work, but I need to use the property "Date" instead of "D_Date".
How can I improve the performance and handle this case?
Join-Object script
/Join-Object Module
:$ListA |FullJoin $ListB -on Date -eq D_Date -Property Date, AAA, BBB
or if the property on both sides isDate
, it is simply:$ListA |FullJoin $ListB -on Date
– Suborbital