GURU SINGH
GURU SINGH

Reputation: 149

Merge two arrays of objects

I am having two variables which are both system.array type. I want to merge them and get the output in Out-grid view.

The first Variable is $out

echo $out

number            : CTASK19496
state             : Assigned
User Name         : Ana
CI Name           :
comments          :
work_notes        :
short_description : Implementation Task
opened_at         : 24/11/2020 01:37:40


number            : CTASK000501
state             : Assigned
User Name         : Ranjay 
CI Name           :
comments          :
work_notes        :
short_description : Implementation Task
opened_at         : 24/11/2020 03:50:16


number            : CTASK000169
state             : Assigned
User Name         : Dan
CI Name           :
comments          :
work_notes        :
short_description : configure the full 
opened_at         : 25/11/2020 14:47:1

The second Variable is $data

echo $data

end_date
--------
10/11/2020 23:59:59
25/11/2020 23:59:59
26/11/2020 23:59:59

code I tried is

$Shortened = $out + $out1 | ForEach-Object {
$_ | Select-Object number, end_date
 }

$Shortened | Format-Table -AutoSize

Output I am getting is

 number        end_date
------        --------
CTASK19496
CTASK000501
CTASK000169
              03/12/2020 23:59:59
              04/12/2020 23:59:00
              30/11/2020 21:30:00

Output what I want is as below

 number        end_date
------        --------
CTASK19496    03/12/2020 23:59:59
CTASK000501   04/12/2020 23:59:00
CTASK000169   30/11/2020 21:30:00

Upvotes: 0

Views: 652

Answers (1)

Doug Maurer
Doug Maurer

Reputation: 8868

Ass Lee_Daily commented, it's assumed each row in each array are related. You can use a for loop like this

for($i = 0; $i -lt $out.count; $i++)
{
    [PSCustomObject]@{
        number   = $out[$i].number
        end_date = $out1[$i].end_date
    }
}

output

number      end_date           
------      --------           
CTASK19496  03/12/2020 23:59:59
CTASK000501 04/12/2020 23:59:00
CTASK000169 30/11/2020 21:30:00

Just capture to a variable and then send to Out-GridView

$output = for($i = 0; $i -lt $out.count; $i++)
{
    [PSCustomObject]@{
        number   = $out[$i].number
        end_date = $out1[$i].end_date
    }
}

$output | Out-GridView

Upvotes: 1

Related Questions