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?
Below is the template showing plants table with the data from schema:
<% project.plants.forEach(function(plant){ %>
<tr>
<td class="td14" ><%= plant.noplant %></td>
<td class="td15" ><%= plant.plantname %></td>
<td class="td16" ><%= plant.planthrperday %></td>
<td class="td17" ><%= plant.plantnodays %></td>
<td class="td18" ><%= plant.planthrrate %></td>
<td class="td19" ><%= plant.plantdep %></td>
<td class="td20" ><%= plant.plantrate %></td>
<td class="td21"><a href=""><button type="button" class="btn btn-danger">Remove</button></a></td>
</tr>
<% }); %>
The route handler:
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});
}
});
});
You can do this in template as I did below by declaring a variable plantRateSum
and adding plantrate
to that variable on every iteration of forEach
<% var plantRateSum = 0; %>
<% project.plants.forEach(function(plant){ %>
<% plantRateSum += plant.plantrate %>
enter code here
<tr>
<td class="td14" ><%= plant.noplant %></td>
<td class="td15" ><%= plant.plantname %></td>
<td class="td16" ><%= plant.planthrperday %></td>
<td class="td17" ><%= plant.plantnodays %></td>
<td class="td18" ><%= plant.planthrrate %></td>
<td class="td19" ><%= plant.plantdep %></td>
<td class="td20" ><%= plant.plantrate %></td>
<td class="td21"><a href=""><button type="button" class="btn btn-danger">Remove</button></a></td>
</tr>
<% }); %>
But this would not be a good approach instead you should send sum of plant rates to template from route handler using reduce
method like below:
const plantRateSum = project.plants.reduce(function(sum, plant){
return sum + plant.plantrate;
}, 0);
Now pass this plantRateSum
to template.
In your route handler do this:
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 {
var plantRateSum = foundProject.plants.reduce(function(sum, plant){
return sum + plant.plantrate;
}, 0);
res.render("cost", {project: foundProject, plantRateSum:plantRateSum});
}
});
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加