2016-07-15 85 views
2

我想從一系列產品中生成購物清單。 我有一個核心數據中的實體「產品」具有名稱(String)和金額(Int)作爲其屬性。我在那一刻,我有一系列的產品有一些重複,這樣的事情:在Swift中過濾對象數組並將其屬性相加

var products : [Product] = [Apple, Egg, Orange, Apple, Orange, Banana, Egg, Egg] 

如何過濾這樣的陣列來獲得特定產品的量的總和?我想要得到如下產品的清單:Apple: 4, Banana: 3, Egg: 7, Orange 2。我知道我可以從該數組中創建一個Set來避免重複,但我不知道如何在此之前對產品的數量進行總結。

+1

爲什麼你有愚弄的陣列?爲什麼當你添加到數組中時你不合並? – Wain

回答

3
enum Product { 
    case Apple 
    case Egg 
    case Orange 
    case Banana 
} 

let products: [Product] = [.Apple, .Egg, .Orange, .Apple, .Orange, .Banana, .Egg, .Egg] 

products.reduce([:]) { (map, product) -> [Product: Int] in 
    var updatedMap = map 
    if let value = map[product] { 
     updatedMap[product] = value + 1 
    } 
    else { 
     updatedMap[product] = 1 
    } 
    return updatedMap 
} //[Orange: 2, Apple: 2, Egg: 3, Banana: 1] 

一樣用繩子:

let products: [String] = ["Apple", "Egg", "Orange", "Apple", "Orange", "Banana", "Egg", "Egg"] 

products.reduce([:]) { (map, product) -> [String: Int] in 
    var updatedMap = map 
    if let value = map[product] { 
     updatedMap[product] = value + 1 
    } 
    else { 
     updatedMap[product] = 1 
    } 
    return updatedMap 
} // ["Apple": 2, "Egg": 3, "Orange": 2, "Banana": 1] 

或者通過擴展爲所有哈希的序列類型:

extension SequenceType where Generator.Element: Hashable { 
    func countElements() -> [Generator.Element : Int] { 
     return reduce([:]) { (map, element) -> [Generator.Element : Int] in 
      var updatedMap = map 
      if let value = map[element] { 
       updatedMap[element] = value + 1 
      } 
      else { 
       updatedMap[element] = 1 
      } 
      return updatedMap 
     } 
    } 
} 

products.countElements() //["Apple": 2, "Egg": 3, "Orange": 2, "Banana": 1] 
-1

你可以這樣做:

enum Fruit { 
    case Orange 
    case Lemon 
    case Potato 
} 

let fruits = [Fruit.Orange, .Lemon, .Potato, .Orange, .Lemon, .Orange] 
let fruitSet = Set(fruits) 
var result = [Fruit: Int]() 

for fruit in fruitSet { 
    result[fruit] = fruits.filter { $0 == fruit }.count 
} 

有人請解釋爲什麼我的回答沒有用? 輸出將是[Fruit.Orange:3,Fruit.Lemon:2,Fruit.Potato:1]

+1

假設您有一個包含20000種不同水果的100萬個元素的數組。您的算法需要多少次遍歷100萬個元素數組? – 7stud

+0

你必須有一個大家庭7stud ....問題是關於一個購物清單。 :) – GOR

0
enum Fruit { 
    case Orange 
    case Lemon 
    case Potato 
} 

let fruits = [Fruit.Orange, .Lemon, .Potato, .Orange, .Lemon, .Orange] 

var result: [Fruit: Int] = [:] 

for fruit in fruits { 
    if let count = result[fruit] { 
     result[fruit] = count + 1 
    } 
    else { 
     result[fruit] = 1 
    } 
} 

print(result) 

--output:-- 
[Fruit.Potato: 1, Fruit.Orange: 3, Fruit.Lemon: 2] 

或者,甚至:

num Product { 
    case Apple 
    case Egg 
    case Orange 
    case Banana 
} 

let products: [Product] = [.Apple, .Egg, .Orange, .Apple, .Orange, .Banana, .Egg, .Egg] 

let result = products.reduce([:]) { (var map, product) -> [Product: Int] in 
    if let count = map[product] { 
     map[product] = count + 1 
    } 
    else { 
     map[product] = 1 
    } 

    return map 
} 

print(result) 

--output:-- 
[Product.Orange: 2, Product.Apple: 2, Product.Egg: 3, Product.Banana: 1] 
相關問題