Angular 如何在typescript中迭代订阅的可观察数据

Angular 如何在typescript中迭代订阅的可观察数据,angular,typescript,Angular,Typescript,这是我的服务方法,在这里我返回可观测值 getAllProperties() { return this.http.get("./assets/data/properties.json"); } 这是我的JSON文件,我正在本地存储在assets文件夹中,并使用HttpClient获取数据 [ { "id": 1, "name": "Real Estate Bldg&quo

这是我的服务方法,在这里我返回可观测值

getAllProperties() {
    return this.http.get("./assets/data/properties.json");
  }
这是我的JSON文件,我正在本地存储在assets文件夹中,并使用HttpClient获取数据

[
    {
        "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
    }
]
当我订阅服务方法时,我不能遍历数据

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);
  //    });

        }
      }
    });

这里有一个对象列表。尝试以这种方式查看对象本身:

this.propertyService.getAllProperties().subscribe(data =>
    
     let json: any;

     if (data) {
         json = JSON.parse(data);   
     } else {
         return;
     }

     for (const obj of json) {
          if (obj) {
               console.log(obj);
          }
     }
});
通过这种方式访问每个字段:

this.propertyService.getAllProperties().subscribe(data =>
     let json: any;

     if (data) {
         json = JSON.parse(data);   
     } else {
         return;
     }

      for (const obj of json) {
          if (obj) {
              for (const key in obj) {
                 if (obj.hasOwnProperty(key)) {
                    console.log(obj[key]); 
                 }
              }
          }
     }
});

谢谢你的回复。这两种情况都给了我这个错误“Type'Object'必须有一个返回迭代器的'[Symbol.iterator]()'方法。”我调整了代码。我忘了您没有从服务器获取普通对象。您必须首先将其解析为JSON对象。您好。这一次,它是说“Object”类型的like参数不可分配给parseOK中“string”类型的参数。让我们回到原点。让我们看看对象数据是什么类型的。请
console.log()。类似这样的内容:
this.propertyService.getAllProperties().subscribe(数据=>console.log(数据))---然后我们将看到如何正确访问JSON。当我使用console.log()数据时,它是这样的[{},{},{}。当我输入(数据)类型时,这是否回答了你的问题?