When it comes to retrieving JSON data using http.get in TypeScript, I've encountered an issue. Specifically, I'm struggling to extract specific values from my key within Angular.
The JSON data format I'm working with looks like this:
[
{
"id": 1,
"name": "Albany",
"manufacture": "Albany Superior Low Gi Sliced Brown Seed Bread 700g",
"price": 15.49,
"category": "Food",
"type": "Breads",
"image": "data:image/jpeg;base64,/9j/4AAQSkZJ..."
},
{
"id": 2,
"name": "Blue Ribbon",
"manufacture": "Blue Ribbon Brown Plus Low Gi Bread 700g",
"price": 13.99,
"category": "Food",
"type": "Breads",
"image": "data:image/jpeg;base64,/9j/4AAQSkZJRg..."
}
]
My service in Angular is structured as follows:
export class ProductService {
prodType: ProductModel;
productList: object;
prodList: Array<ProductModel> = [];
prodMap: Map<number, ProductModel>;
constructor(private http: HttpClient) {}
getAllProducts(): Array<ProductModel>{
this.http.get<Array<ProductModel>>('/product/service/send/all/products').subscribe(
data => {
console.log(data);
},
error => {
console.error("HTTP FAILURE ERROR!!!");
}
);
return this.prodList;
}
getProductByType(productSearch: string){
this.productList = this.prodList.find(x => x.getType() == productSearch);
console.log(this.productList);
}
}
The structure of the ProductModel class is defined as:
export class ProductModel {
private id: number;
private name: string;
private manufacture: string;
private price: number;
private category: string;
private type: string;
private image: string;
// getters and setters
Now, the big question arises: how can I search through my JSON data for product types specifically related to milk and only display those products via console log?
This problem has been a challenge, as similar solutions I have come across were not helpful. Any suggestions or guidance would be greatly appreciated!