在Powershell中,如何检查一个数组中的所有项目是否在第二个数组中? [英] In Powershell how can I check if all items from one array exist in a second array?

查看:421
本文介绍了在Powershell中,如何检查一个数组中的所有项目是否在第二个数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以说我有这个数组:

$requiredFruit= @("apple","pear","nectarine","grape")

然后给我第二个名为$fruitIHave的数组.如何检查$fruitIHave是否包含$requiredFruit中的所有内容.只要$requiredFruit中的所有内容都存在,$fruitIHave中是否还有更多项目都没关系.

And I'm given a second array called $fruitIHave. How can I check that $fruitIHave has everything in $requiredFruit. It doesn't matter if there are more items in $fruitIHave just as long as everything in $requiredFruit is there.

我知道我可以遍历列表,但这似乎效率不高,是否有内置方法可以做到这一点?

I know I could just iterate over the list, but that seems inefficient, is there a built-in method for doing this?

推荐答案

您是否尝试Compare-Object

Do you try Compare-Object :

$requiredFruit= @("apple","pear","nectarine","grape")
$HaveFruit= @("apple","pin","nectarine","grape")
Compare-Object $requiredFruit $haveFruit
InputObject                                                 SideIndicator
-----------                                                 -------------
pin                                                         =>
pear                                                        <=

Compare-Object $requiredFruit $haveFruit | where {$_.sideindicator -eq "<="} | % {$_.inputobject}
pear

这篇关于在Powershell中,如何检查一个数组中的所有项目是否在第二个数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆