Home > Mobile >  Add properties to array
Add properties to array

Time:04-21

Let's say I have an array of objects:

    $data = @(
   [pscustomobject]@{Id1='1';Id2=51213412;ID3='00:01:00'}
   [pscustomobject]@{Id1='1';Id2=51213412;ID3='00:02:00'}
   [pscustomobject]@{Id1='1';Id2=51213412;ID3='00:04:00'}
   [pscustomobject]@{Id1='1';Id2=51213412;ID3='00:04:00'}
   [pscustomobject]@{Id1='1';Id2=51213412;ID3='00:02:00'}
   [pscustomobject]@{Id1='2';Id2=11112314;ID3='00:01:00'}
   [pscustomobject]@{Id1='2';Id2=11112314;ID3='00:02:00'}
   [pscustomobject]@{Id1='2';Id2=11112314;ID3='00:03:00'}
   [pscustomobject]@{Id1='2';Id2=11112314;ID3='00:01:00'}
   [pscustomobject]@{Id1='3';Id2=12512521;ID3='00:01:00'}
   [pscustomobject]@{Id1='2';Id2=11112314;ID3='00:02:00'}
   [pscustomobject]@{Id1='4';Id2=42112521;ID3='00:01:00'}
   [pscustomobject]@{Id1='2';Id2=11112314;ID3='00:03:00'}
   [pscustomobject]@{Id1='3';Id2=12512521;ID3='00:02:00'}
   [pscustomobject]@{Id1='4';Id2=42112521;ID3='00:02:00'}
   [pscustomobject]@{Id1='1';Id2=51213412;ID3='00:03:00'}
   [pscustomobject]@{Id1='4';Id2=42112521;ID3='00:01:00'}
   [pscustomobject]@{Id1='3';Id2=12512521;ID3='00:05:00'}
   [pscustomobject]@{Id1='4';Id2=42112521;ID3='00:03:00'}
   [pscustomobject]@{Id1='3';Id2=12512521;ID3='00:01:00'}
   [pscustomobject]@{Id1='2';Id2=11112314;ID3='00:02:00'}
   [pscustomobject]@{Id1='4';Id2=42112521;ID3='00:01:00'}
   [pscustomobject]@{Id1='2';Id2=11112314;ID3='00:03:00'}
   [pscustomobject]@{Id1='3';Id2=12512521;ID3='00:02:00'}
   [pscustomobject]@{Id1='3';Id2=12512521;ID3='00:04:00'}
   [pscustomobject]@{Id1='1';Id2=51213412;ID3='00:05:00'}
   [pscustomobject]@{Id1='4';Id2=42112521;ID3='00:01:00'}
   [pscustomobject]@{Id1='3';Id2=12512521;ID3='00:05:00'}
   [pscustomobject]@{Id1='5';Id2=53252352;ID3='00:01:00'})

I would like to convert property ID3, that is time to display only minutes so I can ultimately make a sum of them. I found a code that can convert it to minutes:

$minutes = foreach ($mins in $data.ID3) {
$TimeSpan = [System.TimeSpan]::Parse($mins)
[System.Math]::Round($TimeSpan.TotalMinutes,0)}

But now I get the results in $minutes variable. What is the fastest way to replace ID3 values with converted value?

CodePudding user response:

Use a calculated property expression with Select-Object to select a new "column" based on the value of an existing property:

$data |Select-Object ID1,ID2,ID3,@{Name='Minutes';Expression={[math]::Round([timespan]::Parse($_.ID3).TotalMinutes, 0)}}

This will create a new object for every input object, each with the 3 properties from the input object and a new property Minutes with the parsed value

If you want to "overwrite" the existing ID3 property, simply omit it from the list of property names and supply it as the name for the calculated property instead:

$data |Select-Object ID1,ID2,@{Name='ID3';Expression={[math]::Round([timespan]::Parse($_.ID3).TotalMinutes, 0)}}

CodePudding user response:

If you want to update the object's values, you can enumerate them and reassign them like this:

foreach($i in $data) {
    $i.ID3 = ([timespan] $i.ID3).Minutes
}

However, since you mention you want the sum of them, you might be just looking for this:

[timespan]::FromMilliseconds(
    [System.Linq.Enumerable]::Sum(
        [int[]] ([timespan[]] $data.ID3).TotalMilliseconds
    )
).TotalMinutes
  • Related