title: How to get a sum of an array in Nodejs using foreach?
body: <p>I have an array/schema called "plants",and populating on a webpage. I want to get the sum of plant rates(last item). how can i do that in Nodejs?</p>
  
  <p>Below is the template showing plants table with the data from schema:</p>
  
  <pre><code>&lt;% project.plants.forEach(function(plant){ %&gt;
                    &lt;tr&gt;
  
                       &lt;td class="td14" &gt;&lt;%= plant.noplant %&gt;&lt;/td&gt;
                       &lt;td class="td15" &gt;&lt;%= plant.plantname %&gt;&lt;/td&gt;
                       &lt;td class="td16" &gt;&lt;%= plant.planthrperday %&gt;&lt;/td&gt;
                       &lt;td class="td17" &gt;&lt;%= plant.plantnodays %&gt;&lt;/td&gt;
                       &lt;td class="td18" &gt;&lt;%= plant.planthrrate %&gt;&lt;/td&gt;
                       &lt;td class="td19" &gt;&lt;%= plant.plantdep %&gt;&lt;/td&gt;
                       &lt;td class="td20" &gt;&lt;%= plant.plantrate %&gt;&lt;/td&gt;
                       &lt;td class="td21"&gt;&lt;a href=""&gt;&lt;button type="button" class="btn btn-danger"&gt;Remove&lt;/button&gt;&lt;/a&gt;&lt;/td&gt;
  
                     &lt;/tr&gt;
                  &lt;% }); %&gt;
  </code></pre>
  
  <p>The route handler:</p>
  
  <pre><code>    app.get("/myprojects/:id/cost", function(req,res){
  
      Project.findById(req.params.id).populate("labours materials tools plants others").exec(function(err, foundProject){
          if(err){
              console.log(err);
              }else{
            res.render("cost", {project: foundProject});   
          }
  
      });
  
    });
  </code></pre>