This is my service method where i am returning observables.
getAllProperties() {
return this.http.get("./assets/data/properties.json");
}
This is my JSON file which i am storing locally in assets folder and using HttpClient to get data
[
{
"id": 1,
"name": "Real Estate Bldg",
"type": "House",
"price": 12000
},
{
"id": 2,
"name": "Blue Crown",
"type": "House",
"price": 11000
},
{
"id": 3,
"name": "Empire State",
"type": "House",
"price": 19000
},
{
"id": 4,
"name": "Nile Realtor",
"type": "House",
"price": 10000
},
{
"id": 5,
"name": "Benzamin House",
"type": "House",
"price": 21000
},
{
"id": 6,
"name": "Nizel State",
"type": "House",
"price": 45000
}
]
when I am subscribing the service method I can not iterate through data
this.propertyService.getAllProperties().subscribe(data => {
// want to iterate through "data" but it is giving me error Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Object'.No index signature with a parameter of type 'string' was found on type 'Object'.
// if i assign the data to globally declared variable properties:any; and then iterate i can . but what is the problem to iterate through "data" here
for (const key in data) {
if (data.hasOwnProperty(key)) {
console.log(data[key]); // this line is giving red squigly marks
// [solution may be]
//Object.values(data).forEach(element => {
// console.log(element);
// });
}
}
});