I have an array of products and a separate dictionary of inventory. I want to be able to use the keys of the dictionary to "look up" the colorName within the product catalog using the name of the item found in the dictionary. I know you can search for item position within an array, but how to you do this if it is an array within an array?
The products:
The inventory
The products:
Code Block Product(brand: "Academia", category: "Bandages", colorName: [ "Blue", "Green", "Purple", "Red" ], item: ["academiaBandagesBlue", "academiaBandagesGreen", "academiaBandagesPurple", "academiaBandagesRed"])
The inventory
Code Block var tackInventory: [ String : Int] = [ "academiaBandagesBlue" : 1, "academiaBandagesGreen" : 0, "academiaBandagesPurple" : 0, "academiaBandagesRed" : 0
You could do something like this:
and get:
["academiaBandagesBlue"]
key academiaBandagesBlue color Blue
But your coding of Product is not robust at all.
If you do not put color and item in same order, you get wrong result.
You'd better have a dictionary linking item with its color.
You could also define item as a couple of String:
and get
key academiaBandagesBlue ref academiaBandagesBlue color Blue
Code Block struct Product { var brand: String var option: [String] var item: [String] } let products = [ Product(brand: "Academia", option: ["Blue", "Red"], item: ["academiaBandagesBlue", "academiaBandagesRed"]), Product(brand: "Academia", option: ["Purple", "Green"], item: ["academiaBandagesPurple", "academiaBandagesGreen"]) ] var tackInventory: [ String : Int] = [ "academiaBandagesBlue" : 1, "academiaBandagesGreen" : 0, "academiaBandagesPurple" : 0, "academiaBandagesRed" : 0 ] let filteredKeys = tackInventory.filter { $0.value > 0}.map {$0.key} // Select the keys to search for print(filteredKeys) for key in filteredKeys { let filteredProducts = products.filter { $0.item.contains(key)} // all the products that contain that specific item for product in filteredProducts { if let itemPosition = product.item.firstIndex(of: key) { // What is the position of key in item list (there should be only one ? let foundColor = product.option[itemPosition] // If color are in the same order than item, then we got it print("key", key, "color", foundColor) } } }
and get:
["academiaBandagesBlue"]
key academiaBandagesBlue color Blue
But your coding of Product is not robust at all.
If you do not put color and item in same order, you get wrong result.
You'd better have a dictionary linking item with its color.
You could also define item as a couple of String:
Code Block struct Product { var brand: String var item: [(color: String, ref: String)] } let products = [ Product(brand: "Academia", item: [("Blue", "academiaBandagesBlue"), ("Red", "academiaBandagesRed")]), Product(brand: "Academia", item: [("Purple", "academiaBandagesPurple"), ("Green", "academiaBandagesGreen")]) ] var tackInventory: [ String : Int] = [ "academiaBandagesBlue" : 1, "academiaBandagesGreen" : 0, "academiaBandagesPurple" : 0, "academiaBandagesRed" : 0 ] let filteredKeys = tackInventory.filter { $0.value > 0}.map {$0.key} // Select the keys to search for for key in filteredKeys { let filteredProducts = products.filter { product in for item in product.item { if item.ref == key { return true }} return false } //all the products that contain that specific item for product in filteredProducts { for item in product.item where item.ref == key { let foundColor = item.color print("key", key, "ref", item.ref, "color", foundColor) } } }
and get
key academiaBandagesBlue ref academiaBandagesBlue color Blue