I am trying to change a value of a specific item in a hash table. I can do this by iterating through the entire object, testing every single key for a specific value, then changing it if the condition is met like so:
for ($i=0; $i -le $haystack.length-1; $i++)
{
if ($haystack[$i].name -eq "needle")
{
$haystack[$i].currentstatus = "found"
}
}
The above code works but it seems like there has to be a more efficient way to accomplish the task especially when the haystack is large and there is only one needle.
I tried to use where-object and can find the record I'm looking for:
$haystack | where-object {$_.name -eq "needle"}
This seems much better than doing a brute force search but I do not know how to get at that record now. If I had the index in the array then I can easily use that to edit the value I want so is there a way to get the array index? How is this usually done? Thanks.