将不同的数组合并到另一个数组中

时间:2018-03-30 09:54:33

标签: ios arrays swift

struct Product {
    let id: String
    let quantity: Int
    let price: Double
}

class ProductPrices {

    internal static var PricesArray : [String:Double] = ["product1": 5.00,"product2": 5.00,"product3": 5.00]

}

class ProductNames {

    internal static var NamesArray : [String:String] = ["product1": "name1","product2": "name2","product3": "name3"]

}

我有这个数组

["product1","product1","product2,"product1","product3","product2"]

我的问题是如何将这些项目放入Product

预期结果:

Product(id: "name1", quantity: 3, price: 15.00)
Product(id: "name2", quantity: 2, price: 10.00)
Product(id: "name3", quantity: 1, price: 5.00)

3 个答案:

答案 0 :(得分:2)

输入

所以我们有一个结构

struct Product {
    let id: String
    let quantity: Int
    let price: Double
}

我们还有2个词典和1个数组

let prices = ["product1": 5.00, "product2": 5.00, "product3": 5.00]
let names = ["product1": "name1","product2": "name2","product3": "name3"]
let elms = ["product1", "product1", "product2", "product1", "product3", "product2"]

输出

现在我们要使用以下逻辑构建Product(s)数组

    Product.id 检索到
  1. names
  2. Product.quantityProduct.id elms
  3. 的出现次数
  4. Product.price是产品* quantity
  5. 的价格

    我们可以实现写这个

    let products = names.compactMap { elm -> Product? in
        let name = elm.value
        let id = name
        let quantity = elms.filter { $0 == elm.key }.count
        guard quantity > 0, let price = prices[elm.key] else { return nil }
        return Product(id: id, quantity: quantity, price: price * Double(quantity))
    }
    

    测试

    [
        Product(id: "name2", quantity: 2, price: 10.0),
        Product(id: "name1", quantity: 3, price: 15.0),
        Product(id: "name3", quantity: 1, price: 5.0)
    ]
    
      

    Swift 4.1中提供了compactMap方法。对于以前版本的Swift,请使用flatMap

答案 1 :(得分:0)

虽然我对您的数据结构感到困惑,但我向您展示了我的方法。 我相信有更优雅的解决方案,但它按预期工作:

let productArray = ["product1", "product1", "product2", "product1", "product3", "product2"]

// create products
var products: [Product] = []
for uniqueProductName in Set(productArray) {
    let quantity = productArray.filter({$0 == uniqueProductName}).count
    guard let id = ProductNames.NamesArray[uniqueProductName],
        let price = ProductPrices.PricesArray[uniqueProductName] else { continue }
    let total = price * Double(quantity)

    let product = Product(id: id, quantity: quantity, price: total)
    products.append(product)
}

答案 2 :(得分:0)

另一种解决方案可能是

let products = ["product1","product1","product2","product1","product3","product2"]
let (prices, names) = (ProductPrices.PricesArray, ProductNames.NamesArray)

let quantities = products.reduce(into: [:]) { result, product in
    result[product, default: 0] += 1
}

// quantities: ["product2": 2, "product1": 3, "product3": 1]

let result: [Product] =  names.keys.flatMap { key in
    guard let name = names[key],
        let price = prices[key],
        let quantity = quantities[key] else {
            return nil
    }
    return Product(id: name, quantity: quantity, price: price)
}