简体   繁体   English

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

[英]Merging different arrays into another one

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"]

}

I have this array 我有这个数组

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

My question is how can I put these items into Product 我的问题是如何将这些物品放入Product

Expected Result: 预期结果:

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

The input 输入

So we have a struct 所以我们有一个结构

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

We also have 2 dictionaries and 1 array 我们还有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"]

The output 输出

Now we want to build an array of Product(s) using the following logic 现在,我们要使用以下逻辑构建一个Product(s)数组

  1. Product.id is retrieved from names names检索Product.id
  2. Product.quantity is the number of occurrences of Product.id into elms Product.quantityProduct.id进入elms
  3. Product.price is the price of the product * the quantity Product.price是产品的价格* quantity

We can achieve that writing this 我们可以做到这一点

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))
}

Test 测试

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

The compactMap method is available in Swift 4.1. Swift 4.1中提供了compactMap方法。 For previous versions of Swift please use flatMap . 对于以前的Swift版本,请使用flatMap

Although I am confused about your data structures I show you my approach. 尽管我对您的数据结构感到困惑,但仍向您展示了我的方法。 I am sure there are more elegant solutions but it works as expected: 我敢肯定还有更优雅的解决方案,但它可以按预期工作:

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)
}

Another solution could be 另一个解决方案可能是

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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM