Reputation: 817
I have an array of hashtables and I am trying to filter it to those who has a property value to true, but this what I am doing does not looks good.
# object looks like this
$array = @(
@{ Name = 'First'; Passed = $true }
@{ Name = 'Second'; Passed = $false }
)
function Filter {
Param($array)
$filtered = @()
foreach ($item in $array) {
if ($item.Passed = $true) {
$filtered += $item
}
}
return $filtered
}
Is there any other way I can get all elements who has property Passed = $True
without the need to add those to another array.
Upvotes: 0
Views: 439
Reputation: 11188
Just pipe your array into a Where-Object like so:
$array = @(
@{ Name = 'First'; Passed = $True }
@{ Name = 'First'; Passed = $False }
)
$array = $array | Where-Object Passed -EQ $True
Upvotes: 1