I have a service that fetches JSON data from local files. I would like to convert the Object type that http.get returns to this array: Array<{ bookName: string, bookId: number }>. You can see all of my source code in this Github repository.
import { Injectable } from '@angular/core';
import { HttpClient } from "@angular/common/http";
@Injectable({
providedIn: 'root'
})
export class BibleService {
constructor(private http: HttpClient) { }
public fetch(file: string) {
return this.http.get(file);
}
}
bible.component.ts (simplified)
import { Component, OnInit } from '@angular/core';
import { BibleService } from "../bible.service";
@Component({
selector: 'app-bible',
templateUrl: './bible.component.html',
styleUrls: ['./bible.component.scss']
})
export class BibleComponent implements OnInit {
dataBooks: Array<{ bookName: string, bookId: number }>;
constructor(
private bibleService: BibleService,
) {
// fetch JSON data asynchronously
this.bibleService.fetch('./assets/bible/books.json')
.subscribe(response => {
this.dataBooks = response; // how do I map this?
}, error => {
console.error(error);
}, () => {
});
}
ngOnInit() {
}
}
The code is currently working with type any, but I would like to be more explicit with the data.