I'm facing an issue with retrieving specific data fields label
and svm
from a JSON file. The desired fields are nested inside PORTFOLIO > REGROUPEMENT > ELEMENT.
You can access the JSON file here.
I've attempted to display the data using the Google Chrome console.
getTitles(mode: PortfolioModeEnum): void {
this.portfolioValue = 0;
this.lines = [];
this.service.getPortfolios(mode).pipe(
takeUntil(this.unsubscribe$)
).subscribe((res: any) => {
if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
console.log("Test => " + res.REGROUPEMENT[0].ELEMENT[0]);
}
});
}
Encountered error message:
core.mjs:6484 ERROR TypeError: Cannot read properties of undefined (reading '0')
Struggling with data retrieval in a nested loop for LABEL
and SVM
.
portfolio.response.ts
export interface PortfolioResponse extends ApiResponse {
PORTFOLIO: {
VALPTF: number;
DETAILS: {
ACCOUNTLAB: string;
ACCOUNTTYPE: string;
LABEL: string;
ACCOUNTPHYSICAL: string;
};
REGROUPEMENT: InstrumentRegroupement[];
}
}
export interface InstrumentRegroupement {
TYPEVALUE: number;
ASSETCATLABEL: string;
CURRENCY: string;
AMOUNT: number;
PERCENTAGE: number;
ELEMENT: {
LABEL: string;
STOCK: string;
SVM: number;
COUPONNUMBER: number;
ISINCODE: string;
MARKETPLACE: string;
...
}[];
...
EDIT
EDIT 2
TS
getTitles(mode: PortfolioModeEnum): void {
this.portfolioValue = 0;
this.lines = [];
this.service.getPortfolios(mode).pipe(
takeUntil(this.unsubscribe$)
).subscribe((res: any) => {
if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
const data = res.PORTFOLIO.REGROUPEMENT.map((regroupment) => {
return regroupment.ELEMENT.map((element) => ({
label: element.LABEL,
svm: element.SVM,
}));
}).flat();
}
});
}
HTML
Your assistance is greatly appreciated.