In my angular 6 application i am having a product_id
which is an array,
product_id: any = ["123", "456"];
ngOnInit :
ngOnInit() {
this.product_id.forEach(element => {
this.httpClient.get('https://api.myjson.com/bins/hiolc').subscribe(res => {
this.productList = [];
res.products.forEach( item => {
this.productList.push(item);
});
})
})
console.log(this.productList);
}
Here in console.log(this.productList);
, i need to have the expected result as,
[{
"product_name": "Product One",
"product_information": {
"template_details": [
{
"template_name": "RACE Template",
"productProperties": [
{
"property_id": 2518427931,
"property_name": "Length",
"property_value": "12 cm"
},
{
"property_id": 2621195440,
"property_name": "Width",
"property_value": "10 cm"
},
{
"property_id": 2621195441,
"property_name": "Height",
"property_value": "20 cm"
}
]
}
]
}
},
{
"product_name": "Product Two",
"product_information": {
"template_details": [
{
"template_name": "RACE Template",
"productProperties": [
{
"property_id": 2518427931,
"property_name": "Length",
"property_value": "15 cm"
},
{
"property_id": 2621195440,
"property_name": "Width",
"property_value": "12 cm"
},
{
"property_id": 2621195441,
"property_name": "Size",
"property_value": "Medium"
}
]
}
]
}
}]
But which gives empty array []..
How to wait for the service to complete and then store the data into productList
..
I am in the need of forEach, this.product_id.forEach(element
as because in my real application based on the foreach i will get the product list for each product id sent via url..
Kindly help me to store the data after completion of entire service based on forEach
after all the product id sent then get the final product list..
Working stackblitz: https://stackblitz.com/edit/flatternstructure-dvzpjv
Once again i am making it clear that in my real application i need to pass the id to get the product value
like https://api.myjson.com/bins/hiolc + element
..
Also if i get the final productList
then i need to do another functionality with the final productList
so only i am expecting completed data in productList
at last and not inside forEach
..