javascript--Iterative method

Source: Internet
Author: User

<script type= "Text/javascript" >//Five iteration methods all accept two parameters: the function to run on each item and the scope to run the function (optional)//every (): Runs the given function for each item in the array.        Returns True if the function returns true for each item. Filter (): Runs the given function for each item in the array.        Returns an array of items that return true for the function. ForEach (): Runs the given function for each item in the array.        The function does not return a value. Map (): Runs the given function for each item in the array.        Returns a function that consists of the result of each function call. Some (): Runs the given function for each item in the array.        Returns True if the function returns true for either item.        var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; Every () and some () most closely resemble//every () item: The current traversal item, index: Current item, array: The array object itself var Everyresult = numbers.every (function (item, index, array)        {return item > 2;        });            alert (everyresult);//false//some () var someresult = numbers.some (function (item, index, array) {        return item > 2;        });            alert (someresult);//true//filter var filterresult = numbers.filter (function (item, index, array) {        return item > 2;        }); alert (Filterresult);//[3,4,5,4,3]//map () var mapresult = numbers. map (function (item, index, array) {return (item * 2);        }); alert (Mapresult);//[2,4,6,8,10,8,6,4,2]//foreach Essentially, there is no difference between the For loop and Var foreachresult=numbers.foreach (function (i    Tem,index,array) {alert (item)}); </script>

javascript--Iterative method

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.