Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
Assigning part of the returned json data to an array in TypeScript and Angular2

I am currently retrieving json data from a file with various fields - i am interested in 2 fields and each of the 2 field i would like to have them set to different arrays.

SERVICE file:

getCourseType(){
    return this._http.get('url')
     .map((res:Response) => <ICourseType[]> res.json())
     .do(data =>console.log('All: ' + JSON.stringify(data)))
     .catch(this.handleError);
}

COMPONENTS.ts file:

courseType: ICourseType[];
courseName: any[] = [];
courseRoster: any[] = [];

getCourseType(){ //function called from ngOnInit()
    this.dataService.getCourseType().subscribe(
      data => this.courseType = data,
      error => this.errorMessage = <any>error);
  }

JSON file:

[
  {
    "id": 1,
    "courseTitle": "English",
    "courseNumber": 340B,
    "roster": 23,
  },
  {
    "id": 2,
    "courseTitle": "AP History",
    "courseNumber": 1420,
    "roster": 14
  },
  {
    "id": 3,
    "courseTitle": "Art",
    "courseNumber": 42A,
    "roster": 30
  }
]

Currently i am return the json object but if i am interested in gathering all the courseTitle of each of the courses into one array and then all the roster number of each of the courses into one array - where should i do that? In the service or in my components?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In your component file

courseType: ICourseType[];
courseName: any[];
courseRoster: any[];

getCourseType(){ //function called from ngOnInit()
    this.dataService.getCourseType().subscribe(
      data => {
        this.courseType = data
        this.courseType.forEach(course=>{
             this.courseName.push(course.courseTitle);
             this.courseRoster.push(course.roster);
          })  
      },
      error => this.errorMessage = <any>error);
  }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!