Home >Web Front-end >JS Tutorial >JS array learning: traverse the array and double all element values

JS array learning: traverse the array and double all element values

青灯夜游
青灯夜游Original
2021-08-26 16:18:272978browse

In the previous article, we learned about two methods of iterating arrays. If you are interested, you can click on the link to read → "JS array learning iterating arrays to calculate the sum of elements and concatenate the values ​​into strings 》. This time we continue to learn JavaScript arrays and see how to traverse the array and process each element in the array (for example, all values ​​are doubled). Friends in need can learn about it~

Traversing arrays , the first method that comes to mind is to use for loop, with which we can easily double all element values ​​(that is, multiply by 2)

var a = [5,10,20];
for(var i=0;i<a.length;i++){
	a[i]=2*a[i];
}
console.log(a);

Output result:

JS array learning: traverse the array and double all element values

#You can also use the for in statement to traverse the array. In the for/in loop structure, the variable i represents the subscript of the array, and a[i] is the element value that can read the specified subscript.

var a = [10,20,30];
for(var i in a){
	a[i]=2*a[i];
}
console.log(a);

Output result:

JS array learning: traverse the array and double all element values

#You can also use the forEach() method mentioned in the previous article to traverse the array, This method can be called for each element of the array, passing the element to the callback function.

var a = [20,30,40];
function f(value) {
	console.log(value*2);
}
a.forEach(f);

Output result:

JS array learning: traverse the array and double all element values

You can also use the map() method to traverse the array, the map() method can Calls the specified callback function on each element of the array and returns an array containing the results.

array.map(function callbackfn (value, index, array), thisArg);

function callbackfn (value, index, array): A callback function that accepts up to three parameters:

  • value: array element value.

  • index: Numeric index of the array element.

  • array: Array object containing the element.

Let’s learn more about it through code examples:

Example 1: Double the value of all elements (that is, multiply by 2)

var a = [30,40,50];
function f(value) {
	return value*2;
}
var a1=a.map(f);
console.log(a1);

Output result:

JS array learning: traverse the array and double all element values

Example 2: Using JavaScript built-in methods as callback functions

var a = [9, 16];
var a1 = a.map(Math.sqrt);
console.log(a1);  //3,4

Okay, that’s all, you can read it if you need it :javascript video tutorial

The above is the detailed content of JS array learning: traverse the array and double all element values. For more information, please follow other related articles on the PHP Chinese website!

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