Reputation: 677
I just started learning Swift and I come from a Java background. I am looking to figure out how to collect elements of an array of objects to an array. see example below:
struct Employee {
var name: String
var age: Double
var height: Double
var weight: Double
}
let employees = [Employee]()
with the above Employee object, I have a list of employees in an array. I would like to collect all the ages into an array similar to a lambda expression in Java.
In Java 8 you could do:
double[] ages = employees.stream().mapToDouble(x -> x.age).toArray()
thanks in advance
Upvotes: 1
Views: 1335