如果我有2个嵌套循环,如何在内部循环中引用外部循环中的当前项目?可能吗?
$arrayA = @(1..10)
$arrayB = @(11.20)
$arrayA.ForEach({$_; $arrayb.Where({$_ -eq $_})})
代替使用数组方法,请使用cmdlet:
$arrayA | ForEach-Object -PipelineVariable item {
$arrayB.Where{$item -eq $_}
}
# shortened
$arrayA | % -pv item { $_; $arrayB.Where{$item -eq $_} }
-PipelineVariable
是在v4中引入的(您已经基于使用数组方法ForEach
和了Where
)。
虽然是更好的解决方案:
$arrayA.Where{$_ -in $arrayB}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句