Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the summation of arrays by javascript

Shulou Source: shulou.com Published: 2022-06-03 03:29:04 09月20日 Update

This article mainly explains "how to realize the summation of arrays by javascript". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "javascript how to achieve the summation of arrays"!

Methods: 1, use "a.forEach (function (value) {sum+=value})" statement; 2, use "a.reduce (function (pre,curr) {sum=pre+curr})"; 3, use "eval (a.join (" + "))".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript summation of arrays

Method 1: use the forEach () method

The forEach () method invokes each element of the array and passes the element to the callback function.

Example: accumulate array values

Var a = [10,11,12], sum = 0boma.forEach (function (value) {sum + = value;}); console.log (sum)

Output result:

Method 2: use the reduce () method

Var a = [11,12,13], sum= 0. Reduce (function (pre,curr) {sum=pre+curr; return sum;}); console.log (sum)

Method 3: use the eval () method

Var a = [12, 13, 14], sum= 0 (a.join ("+")); console.log (sum)

At this point, I believe you have a deeper understanding of "how to achieve the sum of arrays in javascript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Methods arrays elements content learning practical deeper interest functions practicality practicality simple operation tutorials more friends environment computers examples systems results Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information Huawei MySQL Shulou Technology