Array.Find on powershell array

前端 未结 5 862
耶瑟儿~
耶瑟儿~ 2021-02-13 11:28

How can I use the Array.Find method in powershell?

For example:

$a = 1,2,3,4,5
[Array]::Find($a, { args[0] -eq 3 })

gives



        
5条回答
  •  野性不改
    2021-02-13 12:04

    There is no need to use Array.Find, a regular where clause would work fine:

    $a = @(1,2,3,4,5)
    $a | where { $_ -eq 3 }
    

    Or this (as suggested by @mjolinor):

    $a -eq 3
    

    Or this (returns $true or $false):

    $a -contains 3
    

    Where clause supports any type of objects, not just basic types, like this:

    $a | where { $_.SomeProperty -eq 3 }
    

提交回复
热议问题