A summary of five iterations of the array of JavaScript

Source: Internet
Author: User

ES5 defines five iterative methods, each of which receives two parameters: the function to run on each item and the scope object that runs the function (optional), the scope object will affect the value of this. The functions passed in these methods receive three parameters: the value of the item in the array, the position of the item in the array, and the group object itself.

  1. every () and some ()

    Every () is a given function that runs for each item in the array, and returns True if the function returns true for each item.

    Some () is a given function that runs for each item in an array, and returns True if the function returns true for either item.

    Every () and some () are similar in that they are used to query whether an item in an array satisfies a condition, and for every (), the passed-in function must return true for each item, which returns TRUE, otherwise false. The Some () method returns true whenever the passed-in function returns true for an item in an array. For example:

    var numbers=[1,2,3,4,5,4,3,2,1];   var everyresult=numbers.every (function(item,index,array) {return (item>2 );}); alert (everyresult); // false var someresult=numbers.some (function(item,index,array) {return (item >2);}); alert (someresult); // true

    The above code calls every () and some (), and the incoming function returns true whenever a given item is greater than 2. For every (), it returns false because only some of the arrays are eligible; For some (), the result is true, because at least one item is greater than 2.

  2. filter ()
    filter () runs the given function for each item in the array. Returns an array of items that the function returns True. It uses the specified function to determine whether an item is included in the returned array. For example:
     var  numbers=[1,2,3,4,5,4,3,2,1 ";  var  filterresult=numbers.filter (function   (Item,index,array) { return  ( Item>2 // [3,4,5,4,3];  

    In this example, the incoming function returns an array of all values greater than 2, and creates and returns an array containing 3/4/5/4/3 by calling the filter () method, because the passed function returns true for each of their entries. This method is useful for querying all array items that meet certain criteria.

  3. Map ()
    map () is an array that runs the given function for each item in the array and returns the result of each function call. Each item of this array is the result of running an incoming function on the corresponding item in the original data, for example:
     var  numbers=[1,2,3,4,5,4,3,2,1< Span style= "color: #000000;"  >];  var  mapresult=numbers.map (function   (Item,index,array) { return  Item *2 // [2,4,6,8,10,8,6,4,2]  

    The array returned by the above code contains the result of multiplying each number by 2, which is suitable for creating an array containing the item corresponding to another array of one by one.

  4. ForEach ()
    ForEach () is a given function that runs for each item in most groups, and this method does not return a value. It only runs the incoming function for each item in the array, and there is no return value. is essentially the same as using a for loop to iterate over an algebraic group.
    var numbers=[1,2,3,4,5,4,3,2,1];numbers.foreach (function(iterm,index,array) {         // perform certain actions });
  5. Summarize:
    These array methods can greatly facilitate the task of working with arrays by performing different operations, with browsers that support these iterative methods ie9+, firfox2+, safari3+, Opera9.5, and Chrome.

A summary of five iterations of the array of JavaScript

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.