I am currently working on converting the JSON data received from an API interface into separate arrays containing specific objects. The object type is specified as a variable in the interface.
Here is the interface structure:
export interface Interface{
interfaceClassType: string;
}
The method to retrieve JSON data is as follows:
getJSON(): Observable<Interface[]> {
return this.http.get<Interface[]>(URL)
.pipe(
retry(2),
catchError(this.handleError)
);
}
To implement this, I have created the following code:
arrayWithObjects: Interface[];
class1Array: Class1[];
class2Array: Class2[];
processJSON(): void {
this.configService.getJSON().subscribe(results => this.arrayWithObjects = results);
this.arrayWithObjects.forEach (function (object) {
switch (object.interfaceClassType) {
case "first":
this.class1Array.push(object as Class1);
break;
case "second":
this.class2Array.push(object as Class2);
break;
}
}.bind(this))
}
Even though the above implementation seems correct, calling it triggers an error message:
ERROR TypeError: this.class1Array is undefined