gpt4 book ai didi

arrays - Array.Find和IndexOf用于完全相同对象的多个元素

转载 作者:行者123 更新时间:2023-12-04 10:07:15 27 4
gpt4 key购买 nike

我很难获得完全相同对象的多个元素的当前元素索引:

$b = "A","D","B","D","C","E","D","F"
$b | ? { $_ -contains "D" }

替代版本:
$b = "A","D","B","D","C","E","D","F"
[Array]::FindAll($b, [Predicate[String]]{ $args[0] -contains "D" })

这将返回:
d
d
d

但是这段代码:
$b | % { $b.IndexOf("D") }

替代版本:
[Array]::FindAll($b, [Predicate[String]]{ $args[0] -contains "D" }) | % { $b.IndexOf($_) }

返回值:

1个
1个
1个

所以它指向第一个元素的索引。 如何获取其他元素的索引?

最佳答案

你可以这样做:

$b = "A","D","B","D","C","E","D","F" 

(0..($b.Count-1)) | where {$b[$_] -eq 'D'}

1
3
6

关于arrays - Array.Find和IndexOf用于完全相同对象的多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27690918/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com