typescript - Angular Mapear un objeto anidado observable a otro objeto
Necesito mapear un campo de un subobjeto en el JSON de respuesta al objeto padre
Recibo la siguiente respuesta
{
"id": 1,
"name": "file-1",
"survey": {
"identifier": 1,
"displayedValue": survey-1
}
}
Estoy tratando de mapear el JSON anterior a este objeto
import { Entity } from './entity';
export class File extends Entity {
name: string;
survey: Entity['identifier'];
status: string;
}Aquí está la clase Entity
export class Entity {
displayedValue: string;
identifier: string;
}y aquí es cómo estaba intentando mapearlo
this.fileService.getFileById(this.fileId).subscribe(data => {
this.file = data;
});El método de servicio de archivos
public getFileById(fileId: string): Observable<File> {
const getFileByIdURL = environment.backendBaseURL + 'files/' + fileId;
return this.http.get<File>(getFileByIdURL);
}Necesito que el archivo .survey contenga el identificador de la encuesta
Respuesta #1
Simplemente puedes mapearlo como abajo,
this.fileService.getFileById(this.fileId).subscribe(data => {
const entity = new Entity();
entity.displayedValue = data.survey?.displayedValue;
entity.identifier= data.survey?.identifier;
this.file = new file();
this.file.survey = entity;
this.file.status= 'what u want';
});
