My current task involves retrieving JSON data from a file containing various fields. My focus is on extracting 2 specific fields and storing them in separate 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 receiving the JSON object, but if I want to gather all the courseTitle
values into one array and all the roster
numbers into another array for each course, should I handle that in the service or in my components?