How to realize the Fast Sum of objects by JavaScript
This article will explain in detail how JavaScript realizes the quick summation of objects. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Quick summation of objects
Sometimes we need to sum a property in an array of objects. In the past, we could use a loop traversal method such as forEach or for to calculate it, but now we can use the reduce method to quickly implement
Const objs = [{name:'lilei', score: 98}, {name:'hanmeimei', score: 95}, {name:'polo', score: 85},...] const scoreTotal = objs.reduce ((total, obj) = > {return obj.score + total;}, 0 / * the second parameter is the initial value of total * /)
Using reduce, you can quickly calculate the sum of an attribute.
This is the end of the article on "how to achieve quick summation of objects in JavaScript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.