How to add array elements in JavaScript

青灯夜游
Release: 2022-10-13 16:07:04
Original
9414 people have browsed it

3 implementation methods: 1. Use for loop, syntax "for(i=0;i

How to add array elements in JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Method 1: Use for loop

Implementation idea:

  • Use for loop to traverse the array

  • In the loop body, use the "=" operator to add and sum the array elements one by one

Implementation example:

var arr = [1,2,3,4,5,6,7,8,9,10];
var i,sum=0;
for(i=0;i<arr.length;i++){  //循环遍历数组
	//将数组元素一个个相加求和
	sum+=arr[i];
}
console.log(sum);
Copy after login

How to add array elements in JavaScript

Method 2. Use the forEach() method

The forEach() method is used to call each element of the array element and pass the element to the callback function.

Syntax:

array.forEach(funtion callbackfn(value, index, array), thisValue)
Copy after login

funtion callbackfn(value, index, array): Required parameters, specify the callback function, which can receive up to three parameters:

  • value: The value of the array element.

  • index: Numeric index of the array element.

  • array: Array object containing the element.

thisValue: an omitted parameter, an object that can be referenced by this in the callback function. If thisArg is omitted, the value of this is undefined.

Let’s learn more about it through code examples:

Example: Accumulate and sum array values

var a = [10, 11, 12], sum = 0;
function f(value) {
	sum += value;
}

a.forEach(f);
console.log(sum);
Copy after login

How to add array elements in JavaScript

Method 3. Use the reduce() method

The reduce() method can call the specified callback function for all elements in the array. The return value of this callback function is the cumulative result, and this return value is provided as a parameter the next time the callback function is called.

Syntax:

array.reduce(function callbackfn(previousValue, currentVaule, currentIndex, array), initialValue)
Copy after login

function callbackfn(previousValue, currentVaule, currentIndex, array): Required parameters, specify the callback function, at most Can receive 4 parameters:

  • previousValue: the value obtained by calling the callback function last time. If initialValue is provided to the reduce() method, the previousValue is initialValue when the function is first called.

  • currentValue: The value of the current element array.

  • currentIndex: The numeric index of the current array element.

  • array: Array object containing the element.

initialValue: Omissible parameter, initial value passed to the function.

Let’s learn more about it through code examples:

Example: Accumulate and sum array values

var a = [11, 12, 13], sum = 0;
function f(pre,curr) {
	sum=pre+curr;
	return sum;
}

a.reduce(f);
console.log(sum);
Copy after login

Output results:

How to add array elements in JavaScript

[Related recommendations: javascript video tutorial, programming video

The above is the detailed content of How to add array elements in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template