JS array iteration method_javascript skills

Source: Internet
Author: User
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) script
Map 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.