I've been delving into JavaScript objects lately.
1.
Units = [
{
"BedDetails": [
{
"Outpatient": 2,
"ICU_Beds": 0,
"NL_Beds": 2,
"Extra": 9,
"Ordinary": 3,
"ADMIN": 6,
"SVC_Areas": 3.2 // This is a different kind, not a type of bed
}
]
},
{
"BedDetails": [
{
"Outpatient": 4,
"ICU_Beds": 2,
"Extra": 2,
"Ordinary": 2,
"ADMIN": 2,
"SVC_Areas": 2.1 // This is a different kind, not a type of bed
}
]
}
]
Here are the three operations to perform:
- Sum by types of beds.
- If bed type > 0, then sum SVC_Areas as SVC_bedType.
- Ratio of Bed type / SVC_bedtype.
For example:
- Step 1 sums up bed types.
- Step 2 includes:
If Outpatient > 0, then SVC_outpatient = 2.
If ICU > 0, then SVC_ICU = 0 (since ICU = 0).
- Step 3 calculates the ratio: `(sum of bedtype) / SVC_bedtype` obtained in Step 1/Step 2.
This is what I've done so far:
Units.forEach(function (element) {
var Beds_Details = element.BedDetails[0];
Sum_Outpatient += Beds_Details.Outpatient;
Sum_ICUBeds += Beds_Details.ICU_Beds;
Sum_Extra += Beds_Details.Extra;
Sum_Ordinary += Beds_Details.Ordinary;
Sum_ADMIN += Beds_Details.ADMIN;
if (Beds_Details.Outpatient > 0) { (SVC_Outpatient += Beds_Details.SVC_Areas).toFixed(2); }
if (Beds_Details.ICU_Beds > 0) { (SVC_ICUBeds += Beds_Details.SVC_Areas).toFixed(2); }
if (Beds_Details.Extra > 0) { (SVC_Extra += Beds_Details.SVC_Areas).toFixed(2); }
if (Beds_Details.Ordinary > 0) { (SVC_Ordinary += Beds_Details.SVC_Areas).toFixed(2); }
if (Beds_Details.ADMIN > 0) { (SVC_ADMIN += Beds_Details.SVC_Areas).toFixed(2); }
});
if (SVC_Outpatient > 0) {
RatioModel_Outpatient = (Sum_Outpatient / SVC_Outpatient).toFixed(2);
} else {
RatioModel_Outpatient = 0;
};
// Check other conditions and calculate ratios here...
The code works fine, but I'm looking to optimize it by looping through the Bed_details keys. Can anyone help with this?