I have a piece of code presented below, it takes values from a json file. This is a array of string --> $Json.Names. I would like to avoid duplicate lines like --> $Json.Names[0].Name {$Json.Names[0].Name; break}. Second case is that Array $Json.Names could have different length, array can have 6 and more or less elements. I want to make this switch statement more elastic. I tried to use for loop and while loop, but in this case these loops doesn't help me. Is there any clever method to make this code more sophisticated/elastic and avoid duplicate mentioned code lines$Json.Names[0].Name {$Json.Names[0].Name; break}
$Json = Get-Content "$path" | out-string | ConvertFrom-Json
$Name = switch ($Member) {
$Json.Names[0].Name {$Json.Names[0].Name; break}
$Json.Names[1].Name {$Json.Names[1].Name; break}
$Json.Names[2].Name {$Json.Names[2].Name; break}
$Json.Names[3].Name {$Json.Names[3].Name; break}
$Json.Names[4].Name {$Json.Names[4].Name; break}
$Json.Names[5].Name {$Json.Names[5].Name; break}
$Json.Names[6].Name {$Json.Names[6].Name; break}
default {"Unknown Name"}
}
$Json.Names.Name |Where {$_ -like $member}switch (1..5) { $_ { $_ } }