在 JavaScript 中获取数字数组的总和

在 JavaScript 中获取数字数组的总和

Get the Sum of an Array of Numbers in JavaScript

要获取数字数组的总和:

  1. 使用该Array.reduce()方法遍历数组。
  2. reduce方法中的初始值设置为0
  3. 在每次迭代中,返回累加值和当前数的总和。
索引.js
const arr = [5, 15, 45]; const sum = arr.reduce((accumulator, value) => { return accumulator + value; }, 0); console.log(sum); // 👉️ 65

accumulator参数最初设置为,0因为这是我们作为第二个参数传递给
Array.reduce()
方法的内容。

accumulator在每次迭代中,我们返回当前数组元素的总和。

在最后一次迭代之后,sum变量存储数组中数字的总和。

如果您必须经常计算数组的总和,请定义一个可重用的函数。

索引.js
function calculateSum(array) { return array.reduce((accumulator, value) => { return accumulator + value; }, 0); } const result1 = calculateSum([1, 2, 3]); console.log(result1); // 👉️ 6 const result2 = calculateSum([1, 2, 3, 4]); console.log(result2); // 👉️ 10

calculateSum()函数以一个数组作为参数,并计算数组中数字的总和。

您还可以reduce()使用隐式 return 语句缩短我们传递给方法的函数。

索引.js
function calculateSum(array) { return array.reduce((accumulator, value) => accumulator + value, 0); } const result1 = calculateSum([1, 2, 3]); console.log(result1); // 👉️ 6 const result2 = calculateSum([1, 2, 3, 4]); console.log(result2); // 👉️ 10

示例中的箭头函数使用隐式返回。

另一种可能更简单的方法是使用for...of循环。

for...of使用循环获取数字数组的总和

要获取数字数组的总和:

  1. 声明一个sum变量并将其初始化为0.
  2. 使用for...of循环遍历数组。
  3. On each iteration, reassign the sum variable to its current value plus the
    value of the current element.
index.js
const arr = [5, 15, 45]; let sum = 0; for (const value of arr) { sum += value; } console.log(sum); // 👉️ 65

The
for…of
statement is used to loop over iterable objects like arrays, strings, Map,
Set and NodeList objects and generators.

Notice that we declared the sum variable using the let keyword. Had we declared the variable using const, we wouldn’t be able to reassign it.

On each iteration, we reassign the sum variable to its current value plus the
value of the current element.

If you have to do this often, extract the logic into a reusable function.

index.js
function calculateSum(array) { let total = 0; for (const value of array) { total += value; } return total; } const result1 = calculateSum([1, 2, 3]); console.log(result1); // 👉️ 6 const result2 = calculateSum([1, 2, 3, 4]); console.log(result2); // 👉️ 10

The calculateSum() function takes an array as a parameter, calculates the sum
of its values and returns the result.

You can also use the Array.forEach() method to calculate the sum of an array.

Get the Sum of an Array of Numbers using Array.forEach() #

To get the sum of an array of numbers:

  1. Declare a new sum variable and initialize it to 0.
  2. Use the Array.forEach() method to iterate over the array.
  3. On each iteration, reassign the value of the sum variable to its current
    value plus the array element.
index.js
const arr = [5, 15, 45]; let sum = 0; arr.forEach(value => { sum += value; }); console.log(sum); // 👉️ 65

The function we passed to the
Array.forEach()
method gets called with each element in the array.

On each iteration, we reassign the value of the sum variable to its current value plus the value of the current array element.

The sum variable stores the sum of the values in the array after the last
iteration.

If you have to calculate the sum of an array’s elements often, define a reusable
function.

index.js
function calculateSum(array) { let total = 0; array.forEach(value => { total += value; }); return total; } const result1 = calculateSum([1, 2, 3]); console.log(result1); // 👉️ 6 const result2 = calculateSum([1, 2, 3, 4]); console.log(result2); // 👉️ 10

The calculateSum() function takes an array as a parameter, calculates the sum
of the array’s elements and returns the result.

You can also use a basic for loop to sum an array of numbers.

Get the Sum of an Array of Numbers using a for loop #

To get the sum of an array of numbers:

  1. Declare a new sum variable and initialize it to 0.
  2. Use a for loop to iterate over the array.
  3. Reassign the value of the sum variable to its current value plus the
    current array element.
index.js
const arr = [5, 15, 45]; let sum = 0; for (let index = 0; index < arr.length; index++) { sum += arr[index]; } console.log(sum); // 👉️ 65

We used a basic for loop to iterate over the array.

在每次迭代中,我们使用索引访问当前数组元素并重新分配sum变量。

您选择哪种方法是个人喜好的问题。我会使用
Array.reduce()方法或for...of循环,因为这两个选项都非常直接和直观。