Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use js array to sum

Shulou Source: shulou.com Published: 2022-05-31 10:38:37 10月03日 Update

Most people do not understand the knowledge points of this article "how to use js array to sum up", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use js array summation" article.

Method 1: use a for loop

For (let I = 0; I < array.length; iTunes +) {sum + = array [I];}

Method 2: use the eval () method

Var sum = 0; console.time (); sum = eval (arr.join ("+")); console.timeEnd (); console.log (sum)

Method 3: use the forEach method

Function sum (arr) {var result = 0; arr.forEach (function (arr) {result+= arr;}); return result;}

Method 4: use functional programming map-reduce

Function sum (arr) {return arr.reduce (function (prev, curr, idx, arr) {return prev + curr;});} the above is about "how to sum with js array". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Methods content arrays articles knowledge articles values functions most that is more steps knowledge points industry information information channels channels help loops programming Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Shulou Tech Info Shulou Information MariaDB