I have this product data where I would also need to subtract the quantity of the colors once an order was placed for the specific product.
For now, this is my data structure where the colorMap
is a Map.
Problem: What if I need to have different prices for each of the colors? Should I change the colorMap
into an array?
CodePudding user response:
As far as I understand, the price
of the color is missing. Assuming that you have a different price for each color, the most appropriate way to solve this would be to create inside the document, an array of Color objects called colors
like this:
$docId
|
--- cat: "ML"
|
--- colors (array)
| |
| --- 0
| | |
| | --- colorName: "Black"
| | |
| | --- quantity: 20
| | |
| | --- price: 11.99
| |
| --- 1
| |
| --- colorName: "Green"
| |
| --- quantity: 5
| |
| --- price: 14.99
|
--- price: 200 //Remove that if not necessary
|
--- prodName: "Thumbler"
|
--- size: "500"
In this way, you can simply read the array and calculate the total.
Since you didn't set a tag, we don't know if you are building an app that is for Android, iOS, or web. If by chance it's for Android, here is a useful article: