I have following model
class Foo {
var value: Double
var color: UIColor
init?(value: Double, color: UIColor) {
self.value = value
self.color = color
}
}
How can I sum all value
property inside of [Foo]
using reduce?
It simply like this
let sum = array.reduce(0) { $0 + $1.value }
The same way as with plain numbers:
let foos: [Foo] = ...
let sum = foos.lazy.map { $0.value }.reduce(0, +)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With