In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains how to sum two arrays in ES6. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "How to sum two arrays in ES6"!
Summation methods: 1. Merge two arrays with concat(), syntax "array1.concat (array2)", will add the elements of array2 to the tail of array1;2. Calculate the sum of merged arrays with reduce(), syntax "array.reduce(function(p,c){sum=p+c;})."
Operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
The method of summing two arrays in ES6
Use concat() to merge two arrays into one.
The concat() method concatenates two or more arrays, adding elements of one or more arrays as arguments to the tail of the specified array.
var a = [1,2,3];var b = [4,5,6];console.log(a);console.log(b);var d = a.concat(b);console.log(d);
2. Use reduce() to calculate the sum of all elements in the merged array
Reduce can traverse the array, let the two items before and after the array perform some calculation, then return its value, and continue to calculate, do not change the original array, return the final result of the calculation; if you do not give the initial value, then traverse from the second item of the array.
The reduce() method takes in a function as an accumulator, and each value in the array (from left to right) starts to reduce, eventually computing to a single value.
Summation example:
var a = [1,2,3];var b = [4,5,6];console.log(a);console.log(b);var d = a.concat(b);var sum = 0;d.reduce(function(pre,curr) {sum=pre+curr;return sum;});console.log(sum);
At this point, I believe that everyone has a deeper understanding of "how to sum two arrays in ES6", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.