This article mainly introduces the array Iteration Method of JS. The example analyzes the skills related to the use of array iteration in javascript. The Code has more detailed comments to facilitate understanding and has some reference value, for more information about JS array iteration methods, see the examples in this article. Share it with you for your reference. The specific implementation method is as follows:
Js array Iteration
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 = [,]; var factorial = arr3.reduce (add); // alert (factorial) // 40 var arr4 = [,]; alert (arr4.filter (even) scriptMap and 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 calls a function for each item in the array, and does not return function print (x) {console. log (x * 2)} arr1.forEach (print); // some, the every parameter is a function with a Boolean returned value 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 accumulates until the last one. // Similarly, reduceRight is the first item accumulated from the end. You can see from CONCAT that 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 every. Input a function that returns a Boolean value and returns a new array.
I hope this article will help you design javascript programs.