简体   繁体   English

Angular-7-如何从具有多个字段的Response中创建对象?

[英]Angular - 7 - How to create object from Response having many fields?

How to map and use piple to create new object in Angular ? 如何在Angular中映射和使用piple创建新对象? Below is the response I am getting from Service. 以下是我从“服务”获得的响应。 I only want studentId, name and Title field. 我只需要studentId, name and Title字段。

{
  "studentList": [
    {
      "studentId": "e094208",
      "name": "John Doe",
      "Title": "Senior Engineering",
      "phoneNumbers": [
        {
          "number": "",
          "type": "BUSINESS",
          "public": false
        },
        {
          "number": "",
          "type": "MOBILE",
          "public": false
        }
      ],
      "emails": [
        {
          "email": "john.doe@gmail.com",
          "type": "BUSINESS",
          "public": false
        },
        .......
        .......
        .......
    }
}

Here is the my code. 这是我的代码。 How to convert it into Student[] with three fields? 如何通过三个字段将其转换为Student[]

student = Student[];
this.studentService.searchStudent(name).subscribe((response: any) => {
    this.studentResponse = response.studentList;
});

Try something like this. 尝试这样的事情。

var studentListParsed = JSON.parse('<YOUR JSON>');

const data: Array<Student> = studentListParsed.studentList.map(student => {
return {
    studentId: student.studentId,
    name: student.name,
    title: student.title 
    }
});

You can pipe the data and map to the fields you want in the service. 您可以通过管道传输数据并将其映射到服务中所需的字段。 That is how I prefer to do it: 我更喜欢这样做:

EDIT: Some change to the data structure as per your comment. 编辑:根据您的评论对数据结构进行一些更改。

service: 服务:

import { map } from 'rxjs/operators';

// ....

searchStudent(name): Observable<Student[]> {
  // your function here to fetch data
  .pipe(
    map(data => {
      return data.peopleList.map(student => {
        return <Student>{ eId: student.emplNtId, name: student.name, jobTitle: student.busnTitle }
      })
    })
  )
}

Then in your component just assign the data to your variable: 然后在您的组件中,只需将数据分配给您的变量即可:

this.studentService.searchStudent().subscribe((response: Student[]) => {
  this.studentResponse = response;
  console.log(this.studentResponse)
});

STACKBLITZ STACKBLITZ

PS: Also remember to type your data, avoid using any :) PS:也请记住键入您的数据,避免使用any :)

You can use map function something like 您可以使用类似的地图功能

this.studentService.searchStudent(name).subscribe((response: any) => {
    this.student= response.studentList.map(x =>{
return {studentId :x.studentId ,name : x.name ,title :x.Title } as Student;
});

});

Code Example 代码示例

 var resp={ "studentList": [ { "studentId": "e094208", "name": "John Doe", "Title": "Senior Engineering", "phoneNumbers": [ { "number": "", "type": "BUSINESS", "public": false }, { "number": "", "type": "MOBILE", "public": false } ], "emails": [ { "email": "john.doe@gmail.com", "type": "BUSINESS", "public": false }] } ] } var data=resp.studentList.map(x =>{ return {studentId :x.studentId ,name : x.name ,title :x.Title } }); console.log(data) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将响应对象的字段映射到角度的新数组? - How to Map fields of the response object to new arrays in angular? 如何使用 object 类型的输入字段创建 angular 表单 - How to create angular form with input fields for object type 如何仅从 Http 响应对象中提取某些字段 - How to extract only certain fields from Http response object 如何以角度具有字段值的形式返回嵌套对象数组的特定字段? - How do I return specific field of nested object array in angular having fields value? 如何用 Angular 创建多个页面? - how create many pages with Angular? 根对象具有许多可能的数字选项的动态 Angular 接口 - Dynamic Angular Interfaces With the Root Object Having Many Possible Numeric Options 如何从 angular 9 中的 API 获取字符串响应作为对象? - how to get string response as a object from API in angular 9? 如何从Angular 2中的组件迭代Observable响应对象 - How to Iterate Observable response object from component in Angular 2 如何将响应 object 从 Angular 服务返回到组件的错误请求 - How to return response object for bad request from Angular service to component 如何从 rest api 响应中提取子 object - How to extract sub object from rest api response in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM