I'm banging my head with an issue I'm having with a RXJS transformation in a Ionic 2 application. I'd like to flat a json file into object, this is my semplified json file
[{
"lingua": "it",
"labels": {
"denominazione": "Hi",
},
"tipologie": [
{"denominazione": "test 1"},
{"denominazione": "test 2"}
]
},...]
this is the snippet of typescript code
export class MyRecord{
denominazione: string;
label_denominazione: string;
constructor(denominazione: string, label_denominazione: string) {
this.denominazione = denominazione;
this.label_denominazione = label_denominazione;
}
}
public getRecords() {
var url = '../assets/records.json';
let records$ = this.http
.get(url)
.map(mapRecords);
return records$;
function mapRecords(response: Response): MyRecord[] {
return response.json().filter(x => x.lingua == "it")
.map(({ lingua, labels, tipologie }) => tipologie.map(tipologia =>
toMyRecord(labels, tipologia))
);
}
function toMyRecord(l: any, t: any): MyRecord{
let record= new MyRecord(
t.denominazione,
l.denominazione,
);
return record;
}
When I call the service:
members: MyRecord[];
this.myService.getRecords().subscribe(res => {
this.members = res;
},
err => console.log(err)
);
What I get in this.members is an array of array of MyRecord:
[[{ label_denominazione: "Hi", denominazione: "test 1"}, { label_denominazione: "Hi", denominazione: "test 2"} ]]
instead of an array of MyRecord:
[{ label_denominazione: "Hi", denominazione: "test 1"}, { label_denominazione: "Hi", denominazione: "test 2"} ]
I've tried to use flatMap (mergeMap), but I've got the error 'flatMap is not a function' (btw I've imported the operator mergeMap). I'm also wondering as this.members defined as MyRecord array can accept a different type.