2015-09-17 152 views
1

如何在下列數組的前兩列(a,b)和(c,e)上使用Group-Object數組上的組對象?

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

回答

5

Group-Object接受匿名計算性能,代替屬性名稱:

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

它還接受一個腳本塊:

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

只要表達可以評價爲字符串:

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.