In general, you should create the array for the line_items
key first, then assign it to the dictionary:
var lineItems = [Any]()
for product in array{
lineItems.append(
["product_id": product, "quantity": 1]
)
}
parameters["line_items"] = lineItems
But as Dávid Pásztor said, you can just do a map
:
parameters["line_items"] = array.map {
["product_id": $0, "quantity": 1]
}
If you have a Codable
struct that contains this data,
struct LineItems : Codable {
let lineItems: [Product]
}
struct Product : Codable {
let productId: Int
let quantity: Int
}
I would also recommend that you use the DictionaryEncoder
from this answer to create the entire dictionary.
let encoder = DictionaryEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
let parameters = try encoder.encode(object)