An iterative method for elevation 5.2.8

Source: Internet
Author: User

ECMASCRIPT5 defines 5 iterative methods for an array: every (), filter (), ForEach (), map (), some ()

Each method receives two parameters: the function to run on each item and, optionally, the scope object that runs the function-affects the value of this.

The functions passed in these methods receive three parameters: the value of the array item, the position of the item in the array, and the object itself.

Depending on the method used, the return value after the function is executed may or may not affect the return value of the method.

<!     DOCTYPE html>varnumbers=[1,2,3,4,5,4,3,2,1]; //for every (), each item is true to return True    varEveryresult=numbers.every (function(Item,index,array) {//The function passes in three parameters, the value of the array item, the position of the item in the array index, the array object itself array        return(item>2);    }); Console.log (Everyresult);//false    //about some (), which has a true, returns True    varSomeresult=numbers.some (function(Item,index,array) {return(item>2);    }); Console.log (Someresult);//true    //For filter (), returns an array of true elements for each of the function functions    varFilterresult=numbers.filter (function(Item,index,array) {return(item>2);    }); Console.log (Filterresult);//[3,4,5,4,3]    //about Map (), returns an array of the results of a function call    varMapresult1=numbers.map (function(Item,index,array) {returnItem*2;    }); Console.log (MAPRESULT1);//[2,4,6,8,10,8,6,4,2]    varMapresult2=numbers.map (function(Item,index,array) {return1;    }); Console.log (MAPRESULT2);//[1,1,1,1,1,1,1,1,1]    //with respect to foreach (), it is just the function passed in for each item in the array, which does not return a value, essentially the same as using a for loop to iterate over an algebraic groupNumbers.foreach (function(Item,index,array) {//perform certain actions, such asConsole.log (item);//The output is a loop each output value wraps the next value    }); </script>

An iterative method for elevation 5.2.8

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.