简体   繁体   中英

Group-Object on an array?

How to use Group-Object on the first two columns (a, b) and (c, e) of the following array?

$a = @('a','b','x',10), 
@('a','b','y',20), 
@('c','e','x',50), 
@('c','e','y',30)

Group-Object accepts anonymous calculated properties, in place of property names:

PS C:\> $a | Group-Object @{ Expression={$_[0]} },@{ Expression = {$_[1]} }

It also accepts a ScriptBlock:

PS C:\> $a | Group-Object {$_[0]},{$_[1]}

As long as the expression can be evaluated to a string:

PS C:\> Get-Help Group-Object -Parameter Property

-Property [<Object[]>]
    Specifies the properties for grouping. The objects are arranged into groups 
    based on the value of the specified property.

    The value of the Property parameter can be a new calculated property. 
    To create a calculated, property, create a hash table with an Expression key
    that specifies a string or script block value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM