比较数组时删除匹配项
Remove matches when comparing arrays
我想帮助比较两个数组,比如用户,并抛出两个数组中存在或匹配的任何用户,然后将结果放入最终数组。例如:
###define arrays
$array1 = @("bill","eric","james","sarah")
$array2 = @("bill","scott","sarah","nancy")
###Combine/Filter? arrays and remove users that exist in both arrays
$result = ($array1 + $array2 | some fancy match removal goes here)
$result
eric,james,scott,nancy
我想确保在组合时从两个数组中完全删除匹配项。因此,如果 "sarah" 存在于两个数组中,我想将她从最终结果中完全删除。这可能吗?
使用 Compare-Object
提取在两个源数组中唯一的元素:
$result = Compare-Object $array1 $array2 | Select-Object -Expand InputObject
我想帮助比较两个数组,比如用户,并抛出两个数组中存在或匹配的任何用户,然后将结果放入最终数组。例如:
###define arrays
$array1 = @("bill","eric","james","sarah")
$array2 = @("bill","scott","sarah","nancy")
###Combine/Filter? arrays and remove users that exist in both arrays
$result = ($array1 + $array2 | some fancy match removal goes here)
$result
eric,james,scott,nancy
我想确保在组合时从两个数组中完全删除匹配项。因此,如果 "sarah" 存在于两个数组中,我想将她从最终结果中完全删除。这可能吗?
使用 Compare-Object
提取在两个源数组中唯一的元素:
$result = Compare-Object $array1 $array2 | Select-Object -Expand InputObject