import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/operator/map'; @Injectable() export class ProfileService { idMap: any; profiles: any; constructor(private http: Http) { this.idMap = {}; this.profiles = null; } load() { if (this.profiles) { return Promise.resolve(this.profiles); } return new Promise(resolve => { this.http.get('/assets/data/profiles.json') .map(res => res.json()) .subscribe(data => { this.profiles = data; this.profiles.reduce((map, profile, i) => { map[profile._id] = i; return map; }, this.idMap); resolve(this.profiles); }); }); } getProfiles() { return this.profiles; } getProfileById(id) { return this.profiles[this.idMap[id]]; } }