title: How to iterarte and add in the object by keys
body: <p>I am working with objects in javascript. </p>
  
  <p><strong>1.</strong> </p>
  
  <pre><code>Units = [
      {
          "BedDetails": [
              {
                  "Outpatient": 2,
                  "ICU_Beds": 0,
                  "NL_Beds": 2,
                  "Extra": 9,
                  "Ordinary": 3,
                  "ADMIN": 6,
                  "SVC_Areas": 3.2 // It is different kind not type of bed
              }                  
          ]
      }, 
      {
          "BedDetails": [
              {
                  "Outpatient": 4,
                  "ICU_Beds":2,                
                  "Extra": 2,
                  "Ordinary": 2,
                  "ADMIN":2,
                  "SVC_Areas": 2.1  // It is different kind not type of bed
              }
          ]
      }
  ]
  </code></pre>
  
  <p>Here I need to perform three operations </p>
  
  <ol>
  <li>sum by types of beds </li>
  <li>If type of bed type is > 0 then sum SVC_Areas as SVC_bedType . </li>
  <li>ratio of Bed type / SVC_bedtype.   </li>
  </ol>
  
  <p>For Ex:</p>
  
  <ul>
  <li>step 1 is summing bed types</li>
  <li><p>step 2 is if bedtype > 0 then sum svc under bedtype as below</p>
  
  <p><code>Outpatient &gt; 0  =&gt; SVC_outpatient = 2</code></p>
  
  <p><code>ICU &gt; 0 =&gt; SVC_ICU = 0</code>        //Here ICU = 0 so summing performed</p></li>
  <li>Step 3 is ratio of <code>(sum of bedtype) /  SVC_bedtype</code>   // obtained in step1/step2</li>
  </ul>
  
  <p>Here is I performed until now</p>
  
  <pre><code>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 &gt; 0) { (SVC_Outpatient += Beds_Details.SVC_Areas).toFixed(2); }
      if (Beds_Details.ICU_Beds &gt; 0) { (SVC_ICUBeds += Beds_Details.SVC_Areas).toFixed(2); }                    
      if (Beds_Details.Extra &gt; 0) { (SVC_Extra += Beds_Details.SVC_Areas).toFixed(2); }
      if (Beds_Details.Ordinary &gt; 0) { (SVC_Ordinary += Beds_Details.SVC_Areas).toFixed(2); }
      if (Beds_Details.ADMIN &gt; 0) { (SVC_ADMIN += Beds_Details.SVC_Areas).toFixed(2); }
  });
  
  if (SVC_Outpatient  &gt; 0) { 
     RatioModel_Outpatient  = (Sum_Outpatient  / SVC_Outpatient ).toFixed(2); 
  } else { 
           RatioModel_Outpatient = 0; 
         };
  
  if (SVC_ICUBeds &gt; 0) { 
      RatioModel_ICUBeds = (Sum_ICUBeds / SVC_ICUBeds).toFixed(2); 
  } else { 
          RatioModel_ICUBeds = 0; 
         };               
  
  if (SVC_Extra  &gt; 0) { 
      RatioModel_EX_TR = (Sum_Extra  / SVC_Extra ).toFixed(2); 
  } else { 
            RatioModel_Extra  = 0; 
         };
  
  if (SVC_Ordinary  &gt; 0) { 
      RatioModel_Ordinary  = (Sum_Ordinary  / SVC_Ordinary ).toFixed(2); 
  } else { 
              RatioModel_Ordinary  = 0; 
         };
  
  if (SVC_ADMIN &gt; 0) { 
      RatioModel_ADMIN = (Sum_ADMIN / SVC_ADMIN).toFixed(2); 
  } else { 
           RatioModel_ADMIN = 0; 
          };
  </code></pre>
  
  <p>Although the code is working fine but i need to optimise the code  by loop through the Bed_details keys. </p>
  
  <p>Can any one help for both the problems</p>