title: Mapping two arrays two one object in swift
body: <p>I have two different arrays which am trying to map them to one object, using the information in the first array <code>ModelOne id</code>. I use two for loops to check if the <code>id</code> in model one appears in the second array if true create an object with model one <code>id</code>, <code>name</code> and array of all names in model two. From my implementation am not able to get the correct results.</p>
  <pre><code>// Model One
  struct ModelOne: Codable {
      let id: Int
      let name: String
  }
  
  // Model two
  struct ModelTwo: Codable {
      let id: Int
      let modelOneId: Int
      let name: String
  }
  
  var arrayOne = [ModelOne]()
  
  arrayOne.append(ModelOne(id: 1, name: &quot;One&quot;))
  arrayOne.append(ModelOne(id: 2, name: &quot;Two&quot;))
  
  var arrayTwo = [ModelTwo]()
  
  arrayTwo.append(ModelTwo(id: 1, modelOneId: 1, name: &quot;Some name&quot;))
  arrayTwo.append(ModelTwo(id: 2, modelOneId: 1, name: &quot;Other name&quot;))
  arrayTwo.append(ModelTwo(id: 1, modelOneId: 2, name: &quot;Name one&quot;))
  arrayTwo.append(ModelTwo(id: 2, modelOneId: 2, name: &quot;Name two&quot;))
  
  struct MappedModel {
      let id: Int
      let name: String
      let items: [String]
  }
  
  var arrayThree = [MappedModel]()
  
  for i in arrayOne {
      for x in arrayTwo {
          if i.id == x.id {
              arrayThree.append(MappedModel(id: i.id, name: i.name, items: [x.name]))
          }
      }
  }
  </code></pre>