new to Typescript and trying to get my head around it.
the following is the code that basically generates three random numbers and i use those to select random objects from json.
@Injectable()
export class TrackService {
public drawn :number[]=[];
constructor(private http: Http) {
}
public getRandomTrack(): Observable<Track[]> {
this.generateRandomNumbers();
console.log(this.drawn); //this.drawn is available here
return this.http
.get(API_ENDPOINT)
.map(this.extractRandomData);
}
public extractRandomData(res: Response) {
let body = res.json();
console.log(this.drawn); //undefined here
var randomTrack = [];
for(var i=0;i<this.drawn.length; i++){
randomTrack.push(body.results[this.drawn[i]]);
}
return randomTrack;
}
private generateRandomNumbers(){
var available = [];
for (var i = 1; i<= 55; i++) {
available.push(i);
}
for (var i = 0; i <3; i++) {
var random = Math.floor((Math.random() * available.length));
this.drawn.push(available[random]);
available.splice(random, 1);
}
return this.drawn;
}
as you see inside extractRandomData this.drawn is undefined, how do i pass the this.drawn into that function so i can access that array.
Please help.