title: How to add data inside two arrays inside for loop
body: <p>I'm trying to add data to array inside for loop and again to array in another for loop.
  Its working on first for loo, but in second data is empty. How to fix that</p>
  <p>This is how it currently looks inside firestore and you can see scores missing.
  <a href="https://i.stack.imgur.com/4DjPb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4DjPb.png" alt="How it looks inside firestore" /></a></p>
  <p>this is code:</p>
  <pre><code>func updateCourseData() {
  
      var data = [&quot;endTime&quot;: Timestamp(date: Date()),
                  &quot;players&quot;: []] as [String : Any]
  
      for i in 0..&lt;players.count {
  
        var playerData = [&quot;playerId&quot;: games[i].id,
                          &quot;scores&quot;: []] as [String : Any]
  
        var existingItems = data[&quot;players&quot;] as? [[String: Any]] ?? [[String: Any]]()
  
        existingItems.append(playerData)
  
        data[&quot;players&quot;] = existingItems
  
        for score in 0..&lt;selectedPage + 1 {
  
          let scoreData = [&quot;hole&quot;: games[i].scores[score].hole,
                           &quot;score&quot;: games[i].scores[score].score] as [String : Any]
  
          var existingScores = playerData[&quot;scores&quot;] as? [[String: Any]] ?? [[String: Any]]()
  
          existingScores.append(scoreData)
  
          playerData[&quot;scores&quot;] = existingScores
        }
      }
  
      Constants.FirebaseCollection.gamesCollection.document(documentId).updateData(data) { error in
              if let error = error {
                print(error.localizedDescription)
              }
              print(&quot;Game updated&quot;)
            }
  }
  </code></pre>