Question:
Assuming:
1 2 3 4 5 6 |
$columnArray = @( @{Name='COL_A';Type=[int];Measurements=@()}, @{Name='COL_B';Type=[string];Measurements=@()}, @{Name='COL_C';Type=[datetime];Measurements=@()} ) |
Using the Where-Object
command to get COL_B
‘s Measurements
array:
1 2 |
($Fields | ? { $_.Name -eq 'COL_B' }).Measurements |
Is there a more-succinct way of doing this? Would a nested set of PSCustomObject
s be a better approach?
Answer:
If you are interested in looking element by key, than Hashtable
will be better approach than array.
1 2 3 4 5 6 |
$columnHashtable=@{ COL_A=@{Type=[int];Measurements=@()} COL_B=@{Type=[string];Measurements=@()} COL_C=@{Type=[datetime];Measurements=@()} } |
With Hashtable
you can get Measurements
array of COL_B
by this command:
1 2 |
$columnHashtable.COL_B.Measurements |
And If you want to enumerate all elements in Hashtable
, then you can use following command:
1 2 |
$columnHashtable.GetEnumerator()|ForEach-Object ... |
In case if order of elements in hash table matters, you could use [ordered]
operator before hash table literal to create OrderedDictionary
instead of Hashtable
.