title: How to generate a 2D-array from a 1D-array using map and filter?
body: <p>I have the desired output using a loop today but how can I exchange this loop with a one line expression using map and filter?</p>
  
  <p>I start with an array of countries. From this one I generate a second array with the initial letters using map. I would now like to build a 2D-array that contains all the countries with the same initial letter in separate arrays using map and filter so that I can get rid of the loop I use today.</p>
  
  <pre><code>let countries = ["Albania", "Algeria", "Angola", "Bahamas", "Bahrain", "Canada"]
  
  var initials = Set(countries.map { $0.prefix(1) }).sorted()
  
  func countriesByInitial() -&gt; [[String]] {
      var result = [[String]]()
      for initial in initials {
          result.append(countries.filter { $0.prefix(1) == initial })
      }
      return result
  }
  </code></pre>