title: SwiftUI Can't Pass Variable to Higher Order Function Map
body: <p>I have an app with an array of models that contain number fields. I'd like to be
  able to sum any given data field. I can do so with map by using the data field
  name ($0.cups for example). However I would like to programmatically send in a
  variable for &quot;cups&quot; and nothing I have tried works.</p>
  <p>Here's some example code. I'd like to pass the field name that I want to total into
  the function sumOnAttribute</p>
  <pre><code>struct ContentView: View {
  
      @State private var answer: String = &quot;&quot;
  
      @State private var startingModels = [
          StartingModel(name: &quot;abe&quot;, age: 1, bags: 2, cups: 3),
          StartingModel(name: &quot;abe&quot;, age: 11, bags: 12, cups: 13),
          StartingModel(name: &quot;abe&quot;, age: 21, bags: 22, cups: 23),
          StartingModel(name: &quot;donna&quot;, age: 31, bags: 32, cups: 33),
          StartingModel(name: &quot;elsie&quot;, age: 41, bags: 42, cups: 43),
          StartingModel(name: &quot;farah&quot;, age: 51, bags: 52, cups: 53)
      ]//some data
  
      var body: some View {
      
          VStack(spacing: 10) {
              Text(&quot;Do Some Math&quot;)
                  .font(.title)
              Text(answer)
                  .font(.headline)
              Button {
                  answer = String(sumOnAttribute(filterField: &quot;abe&quot;, sumField: &quot;cups&quot;))
              } label: {
                  Text(&quot;Make it So&quot;)
              }
              .font(.system(size: 20))
          }//v
      }//body
  
      func sumOnAttribute(filterField: String, sumField: String) -&gt; Double {
      
          let filteredAttribute = startingModels.filter({ $0.name == filterField })
      
          //$0.sumField does not work, $0.value(sumField) does not work
          let nums = filteredAttribute.map({ $0.cups })
  
          let total = nums.reduce(0, +)
          return total
      }
  }//struct
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  
  struct StartingModel: Identifiable {
      let id = UUID()
      let name: String
      let age: Double
      let bags: Double
      let cups: Double
  }//model
  </code></pre>
  <p>Seems like this should be simple, but I have not found any answer in a lot of searches.
  Any guidance would be appreciated. Xcode 13.3, iOS 15.4</p>