title: Swift iOS -Compare elements in 1 array by property
body: <p>I have an array of objects and I want to compare the objects based on property to find out if the properties are all the same. Right now I loop through all the objects, place all values of the properties in a separate array, and then use <code>filterArr.allSatisfy { $0 == filterArr.last }</code> to detemermine wether the properties are all the same or not. </p>
  
  <p>This method works but I know there has to be a more elegant way then what I'm doing.</p>
  
  <p>I actually went looking for an answer to this but every single thing I came across was comparing the elements of 2 different arrays instead of 1.</p>
  
  <pre><code>class IceCream {
      var flavor: String?
      var price: Double?
  }
  
  let iceCream1 = IceCream()
  iceCream1.flavor = "vanilla"
  iceCream1.price = 1.5
  
  let iceCream2 = IceCream()
  iceCream2.flavor = "chocolate"
  iceCream2.price = 2.0
  
  let iceCream3 = IceCream()
  iceCream3.flavor = "vanilla"
  iceCream3.price = 1.5
  
  let iceCream4 = IceCream()
  iceCream4.flavor = "strawberry"
  iceCream4.price = 2.5
  
  let iceCreams = [iceCream1, iceCream2, iceCream3, iceCream4]
  
  var filterArr = [String]()
  
  for iceCream in iceCreams {
      filterArr.append(iceCream.flavor ?? "")
  }
  
  let areItemsEqual = filterArr.allSatisfy { $0 == filterArr.last }
  
  print(areItemsEqual) // prints false
  </code></pre>