I am trying to change the value of a specific item in a hash table. I can do this, iterate over the entire object, test each individual key for a specific value, and then change it if the condition is true like this:
for ($i=0; $i -le $haystack.length-1; $i++) { if ($haystack[$i].name -eq "needle") { $haystack[$i].currentstatus = "found" } }
The above code works, but there seems to be a more efficient way to complete the task, especially when the haystack is large and there is only one needle.
I tried using where-object and can find the record I'm looking for:
$haystack | where-object {$_.name -eq "needle"}
It seems a lot better than brute force, but I donβt know how to get to this record. If I had an index in the array, then I could easily use this to edit the value I want, and is there any way to get the index of the array? How is this usually done? Thanks.
Wd-40 source share