'How to add value from Dictionary having value as dictionary in Swift?
I am having dictionary whose value is another dictionary
for ex. ["1": ["1623699578": 1.08], "2": ["1216047930": 6.81]]
inner dictionary will have only one value
i want to do sum of 1.08 and 6.81
for now i am tried to just iterate dictionary and taking value using dict.values.first and adding. But i not sure if its best way
is there any best way to do this?
Solution 1:[1]
Another notation for what @George proposed can be:
let sum = dict.values.compactMap { $0.values.first }.reduce(0.0) { $0 + $1 }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | HunterLion |
