如何获取具有特定值的哈希表键? [英] How to get hash table key which has specific value?
问题描述
我有一个哈希表,其中根据值使用键.
I am having a hash table where Keys are being used based on value.
例如.
$ComponentTobeBuild=@{"ComponentNameX"="True";
"ComponentNameXyz"="False";
"SomeComponent"="False"}
我想获取值为 True 的键.(我会将密钥作为参数传递给其他脚本).
I would like to get the keys which are having values True. (I will pass the key to some other script as parameter).
我是这样尝试的,但我认为我缺少某些地方,因为它没有列出密钥.
I was trying like that , But i think some where i am missing as it is not listing the keys.
$($ComponentToBuild.Keys) | Where-Object { $_.Value -eq "True" }
如何获取表示为True的组件名称?另外我想知道哈希表是否是此类工作的明智选择.因为我认为哈希表将主要用于处理值.
How to get the component Name which are having denoted as True? Also i would like to know whether hash table is a wise choice for this kind of work. Because I thought that Hash table will be mainly be used for processing the values.
推荐答案
这应该可以满足您的需求.
Hi this should work for what you want.
$ComponentTobeBuild=@{"ComponentNameX"="Test";
"ComponentNameXyz"="False";
"SomeComponent"="False"}
Foreach ($Key in ($ComponentTobeBuild.GetEnumerator() | Where-Object {$_.Value -eq "Test"}))
{$Key.name}
这篇关于如何获取具有特定值的哈希表键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!