Array.Find on powershell array

前端 未结 5 871
耶瑟儿~
耶瑟儿~ 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:09

    Another option would be using an ArrayList, which provides a Contains method:

    PS C:\> [Collections.ArrayList]$a = 'a', 'b', 'c'
    PS C:\> $a.Contains('b')
    True
    PS C:\> $a.Contains('d')
    False

    Or, as @Neolisk mentioned in the comments, you could use PowerShell's -contains operator:

    PS C:\> $a = 'a', 'b', 'c'
    PS C:\> $a -contains 'b'
    True
    PS C:\> $a -contains 'd'
    False

提交回复
热议问题