'Swift How to break an array of objects into an array of arrays?
I have an array of purchase objects [Purchase] defined as:
struct Purchase {
let count: Int
let food: String
}
How can I get turn this array of purchases into an array of arrays of purchases with the same count (Int)?
For example:
let input: [Purchase(count: 2, food: "popcorn"), Purchase(count: 3, food: "popcorn"), Purchase(count: 2, food: "soda"),
Purchase(count: 2, food: "popcorn"), Purchase(count: 2, food: "soda")]
output: [[Purchase]] = [[Purchase(count: 2, food: "popcorn"), Purchase(count: 2, food: "soda"),
Purchase(count: 2, food: "popcorn"), Purchase(count: 2, food: "soda")], [Purchase(count: 3, food: "soda")]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
