In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail "es6 how to achieve array summation", the content is detailed, the steps are clear, the details are handled properly, I hope this "es6 how to achieve array summation" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
Es6 method to achieve array summation: 1, using the reduce () method, syntax "arr.reduce (function (pjournal c) {sum=p+c;});"; 2, using the forEach () method, syntax "arr.forEach (function (v) {sum+=v})".
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
In es6, you can use the new array traversal methods reduce () and forEach () to sum the array. Let's take a look at the details.
Method 1: use the reduce () method
Reduce can traverse the array, let the front and back items of the array do some calculation, then return its value, and continue the calculation without changing the original array, returning the final result of the calculation; if no initial value is given, the traversal starts with the second term of the array.
The reduce () method takes a function as an accumulator, and each value in the array (from left to right) starts to shrink and eventually evaluates to a value.
Example: use the reduce () method for array summation
Var arr = [11, 12, 13], sum= 0 position arr.reduce (function (pre,curr) {sum=pre+curr;return sum;}); console.log (sum)
Method 2: use the forEach () method
The forEach () method invokes each element of the array and passes the element to the callback function.
In the callback function, you can do a sum operation to add up all the elements of the array passed in.
Example: accumulate array values
Var arr = [1,2,3], sum = 0posiarr.forEach (function (value) {sum + = value;}); console.log (sum)
After reading this, the article "how to realize the summation of arrays in es6" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.