Trying to extract and display data from the balanceModel parameter in the function is proving challenging due to deep nested models. Any help in identifying any flaws in the types below would be greatly appreciated.
Explore a live example of this logic on CodeSandbox:
https://codesandbox.io/s/bold-meitner-vxto9
type BalanceModelAMDRType = {
from: number;
to: number;
[index: string]: number;
};
type BalanceModelSectionType = {
DRI: {
AI: number;
AMDR: BalanceModelAMDRType;
EAR: number;
RDA: number;
UL: number;
unit: string;
[index: string]: string | number | BalanceModelAMDRType;
};
};
type BalanceModelProgressSectionType = {
DRI: {
recommended: number;
unit: string;
[index: string]: string | number;
};
};
type BalanceModelType = {
energy: BalanceModelSectionType;
[index: string]: BalanceModelSectionType | BalanceModelProgressSectionType;
};
function _updateEnergyDependentSections(
balanceModel: BalanceModelType,
energy: number
): void {
const sections = [`mock`, `data`];
sections.forEach(sectionName => {
if (balanceModel[sectionName]) {
const { DRI } = balanceModel[sectionName];
Object.keys(DRI).forEach(DRIName => {
switch (true) {
case sectionName === `mock`:
const AMDR = DRI[DRIName];
Object.keys(AMDR).forEach(AMDRValueName => {
const AMDRValue = AMDR[AMDRValueName];
AMDR[AMDRValueName] = Math.round(
AMDRValue * conversionMultiplier
);
});
break;
case sectionName === `data`:
DRI[DRIName] = Math.round(DRI[DRIName] * conversionMultiplier);
}
});
}
});
}