Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda