Filtering Values Out of JSON

Hello,

I have a JSON file containing the data below. I was wondering how can I just get the Team Names only once. I am able to get the records and display in my picker but it comes out like this. I know why it does it but I just cant figure out how to get just the unique values. Still very new to this.

_**_If I filter on Tee Ball I get

Team #1 Team #1 Team #2_**_

_**or if I filter on Coach Pitch I get

Team #10 Team #10 Team #2**_

I want it to only display the teams once so if I pass in Tee Ball it only shows

Team #1 Team #2

[
 {
   "TeamName": "Team #1",
   "DivisionName": "Tee Ball",
   "PlayerFirstName": "Joe",
   "PlayerLastName": "Smith"
 },
 {
   "TeamName": "Team #10",
   "DivisionName": "Coach Pitch",
   "PlayerFirstName": "Jane",
   "PlayerLastName": "Jones"
 },
 {
   "TeamName": "Team #10",
   "DivisionName": "Coach Pitch",
   "PlayerFirstName": "Peter",
   "PlayerLastName": "Parker"
 }, {
   "TeamName": "Team #1",
   "DivisionName": "Tee Ball",
   "PlayerFirstName": "Reed",
   "PlayerLastName": "Richards"
 }, {
   "TeamName": "Team #2",
   "DivisionName": "Tee Ball",
   "PlayerFirstName": "Brent",
   "PlayerLastName": "Barry"
 }, {
   "TeamName": "Team #2",
   "DivisionName": "Coach Pitch",
   "PlayerFirstName": "Harry",
   "PlayerLastName": "Hunt"
 },
 
]



Code to pull JSON data

   func getLocalData() {
     
    // Get a url to the jsons file
    let jsonsUrl = Bundle.main.url(forResource: "Book2", withExtension: "json")
     
    do{
      // Read the file into a data object
      let jsonData = try Data(contentsOf: jsonsUrl!)
       
      //Try to decode the json into an array of players
      let jsonDecoder = JSONDecoder()
      let players = try jsonDecoder.decode([Player].self, from: jsonData)
       
      //Set unique IDs for each instance
      for p in players {
         
        p.id = UUID()
        // Set a unique ID for each recipe in the player array
         
      }
       
     
       
      // Assign parsed players to players property
      self.players = players
       
    }
    catch{
      // TODO log error
      print("Couldn't parse local data")
    }
```

Picker in my view





`Picker("Please select a team", selection:$selectedTeam) {
          if buttonColorFlag == false {
            Text("Select a Team").tag(selectedTeam)
          }
            ForEach(player.players) {player in
                    if player.DivisionName == selectedDivision{
                      Text(player.TeamName!).tag(player.TeamName!)
                    }
                  }
        }
        .multilineTextAlignment(.leading)
        .frame(width: 200)`


Thanks in advance

You can use a combination of .map and Set here to quickly get the team names only once.

Here's what that looks like:

let teamNames = Set(players.map { $0.TeamName })
// teamNames is: ["Team #10", "Team #2", "Team #1"]

This works because .map runs the code $0.TeamName on every Player in the array, making it into an array of team names. This doesn't fully solve the problem though, as we still have multiple mentions of each team. To fix this, you can use a Set. Sets are similar to Arrays, but with elements must be unique.

If you wanted to take it a step further, you can also use a Dictionary to group your players by their team. This code will create a dictionary containing arrays of players, keyed by their team name:

let groupedPlayers = Dictionary(grouping: players, by: { $0.TeamName })
/* groupedPlayers is: [
     "Team #2": [
         Player(TeamName: "Team #2", DivisionName: "Tee Ball", PlayerFirstName: "Brent", PlayerLastName: "Barry"),
         ...
     ],
     "Team #1": [
         Player(TeamName: "Team #1", DivisionName: "Tee Ball", PlayerFirstName: "Joe", PlayerLastName: "Smith"),
         ...
     ],
     "Team #10": [
         Player(TeamName: "Team #10", DivisionName: "Coach Pitch", PlayerFirstName: "Jane", PlayerLastName: "Jones"),
         ...
     ]
] */

I'd imagine storing the players like that could come in handy somewhere down the line. Hope this helps!

Filtering Values Out of JSON
 
 
Q