首页 > 解决方案 > 尝试在 Angular 6 中使用对象 Object 作为数组

问题描述

这是我的问题的一些背景

我正在用 Angular 6 编写一个应用程序,它应该从我的 localhost:5000 端口读取数据以获取 JSON 数据并将其显示在 Angular 应用程序中。角度应用程序完美地从我的本地主机获取数据,除非收集数据时,整个数据是一个“对象 Object”,我不知道如何迭代这种类型或可视化收集的数据。我知道数据被正确收集的原因是发布数据的服务器收到了 200 条成功消息。

这是我的角度应用程序正在接收的数据:

{"index":1,"Company":"Google","Hire Date":"1\/4\/16","Title":"Director"
,"Location":"Bay Area, CA","Degree":"Bachelors","Year entered 
work force":1994.0,"Offer; Base":"$225,000.00 
","Bonus":null,"Latitude":37,"Longitude":-122}

这是我在 Angular 中拟合数据的数据模型:

  export interface Exam {
    ID: number;
    Company: string;
    HireDate: string;
    Title: string;
    Location: string;
    Degree: string;
    yearEnteredWorkForce: string; 
    OfferCostNumber: string;
    bonus: string;
    latitude: number;
    longitude: number;
  }

这是作为“object Object”返回的变量

this.examsList = this.examsApi.getExams()

标签: angularobject-object-mapping

解决方案


import { Observable, Subscription } from 'rxjs/Rx'; /* 这应该添加到文件的顶部 */

this.examsApi.getExams().subscribe(res => {this.examsList = res)
       /* Write code to do operation on your response object.*/
});

您将不得不使用 subscribe 方法,您将获得响应对象而不仅仅是[object, Object]


推荐阅读