Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda