This article describes the array iteration method of JS. Share it for your reference. The specific implementation method is as follows:
<!doctype html><html><head lang="zh"> <meta charset="utf-8"> <title>js array iteration</title> <meta name="renderer" content="webkit"> <script> var arr1 = [1,2,3,4,5,6]; function double(x){ return 2*x; } // map can generate a new array// alert(arr1.map(double)); function print(x){ console.log(x*2) } arr1.forEach(print); function even(x){ return x %2 ==0 } var arr2 = [2,4,,5,6]; // alert(arr2.every(even))//false; // alert(arr2.some(even))//true; function add(a,b){ return a*b; } var arr3=[1,2,4,5]; var factorial = arr3.reduce(add); //alert(factorial) //40 var arr4=[1,24,5,6,7,8,234,4]; alert(arr4.filter(even)) </script> <pre> map,filter can generate a new array var arr1 = [1,2,3,4,5,6]; function double(x){ return 2*x; } //alert(arr1.map(double)); //forEach is to call a certain function for each item in the array, without returning function print(x){ console.log(x*2) } arr1.forEach(print); //some,every parameter is a function that returns a boolean value function even(x){ return x %2 ==0 } var arr2 = [2,4,,5,6]; // alert(arr2.every(even))//false; // alert(arr2.some(even))//true; //reduce accepts a function, returns a value, and constantly accumulates to the last item //Similarly, reduceRight is accumulated from the latter to the first item. For details, it can be seen from CONCAT function add(a,b){ return a*b; } var arr3=[1,2,4,5]; var factorial = arr3.reduce(add); //alert(factorial) //40 //filter is similar to everything, registers a function that returns a boolean value and returns a new array</pre></body></html>I hope this article will be helpful to everyone's JavaScript programming.