I am working with the following JSON data:
{
"StatusCode": 0,
"StatusMessage": "OK",
"StatusDescription": [
{
"_id": "12123",
"dateCreated": "2019-12-03T13:45:30.418Z",
"pharmacy_id": "011E752345553380ABC13FFA163ECD15"
"products": [
{
"productID": "1",
"quantity": 3,
"product_name": "BETADINE",
"price": 10
},
{
"productID": "2",
"quantity": 1,
"product_name": "EUCARBON",
"price": 10
}
]
},
{
"_id": "56233",
"dateCreated": "2019-12-04T09:55:21.555Z",
"pharmacy_id": "011E762345552280FBC13FFA163ECD10"
"products": [
{
"productID": "44",
"quantity": 1,
"product_name": "BETADINE",
"price": 10
}
]
}
]
}
My goal is to extract the total number of products in this JSON structure. For example, in this case, there are 2 pharmacies and a total of 5 products. I want to display this count in my shopping cart.
To achieve this, I'm using the following function to fetch the JSON data from an API:
public cart: Shop[];
cartlength: number;
ngOnInit(): void {
this.shopservice.getShoppingCart().subscribe(
cart => {
this.cart = cart;
this.cartlength = cart.length;
},
err => console.error('errorrrrrrr', err),
() => console.log('error')
);
}
In my HTML file, I am displaying the product count using the following code snippet:
<ActionItem ios.systemIcon="9" ios.position="left" android.position="actionBar" [text]='cartlength'></ActionItem>
Any suggestions or ideas on how to improve this implementation?