Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the summation of arrays by javascript

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

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: 286

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report