Several built-in functions for array arrays in JavaScript

Source: Internet
Author: User
Tags prev

This article is referenced in the third edition of JavaScript advanced programming

Today, while looking at JavaScript books, I saw some of the built-in function objects of the array in JavaScript that I didn't know before, so I wrote this essay in order to make it easy to query in the later development work. Direct Sticker Code,

functionArrayeffect () {varnumbers = [1,2,3,4,5,6,7,8,9,10]; //------------------------------------Support browser version ie9+,firfox, Safair 3+,opera 9.5+,chrome------------------------ ------------                                                /** Every iteration method, which runs the given function for each item in the array, and returns True if the function is true for the return value of None*/                varever = Numbers.every (function(Item,index,array) {returnItem-1 = =index;                                }); /** Filter method, Investor Incredibles, similar to filter, returns true for an array of items **/                varFilter = Numbers.filter (function(Ele,index,array) {returnEle% 3 = = 0;                                }); /** An array of each of the given functions in the array that returns the result of each function call*/                varMap = Numbers.map (function(Ele, Index,array) {returnEle *Ele;                                }); /** Each item in the array is given a function, if any of the functions returns True, returns True, otherwise false*/                varsome = Numbers.some (function(Ele,index,array) {returnEle% 2 = = 0;                }); //----------------------------------------------------------------------------------------------------------- --------------------------                                                /** Receive two parameters, the function called on each item and the initial value as the avoidance basis (the first item is optional) * Call function has four parameters, previous value, current value, current element array position, and arrays Object * Tips: The return value of each direction in the execution function will be the first parameter of the next execution function * such as the first execution of the callback, Prev is 1,cur 2. The second prev is 3 (1 + 2 results), and Cur is 3 (the third item). Until the last array element * Supports browser version ie9+, Firfox, Safair 4+,opera 10.5,chrome*/                varreduce = Numbers.reduce (function(Prev,cur,index,array) {returnPrev +cur; },34); /** Receive two parameters, the function called on each item and the initial value as the avoidance basis (the first item is optional) * The calling function has four parameters, the previous value, the current value, the current element array position, and the group object (with the reduce reciprocal operation, that is, the total last item starts to the front Convenience elements, not otherwise different) * Support browser version ie9+, Firfox, Safair 4+,opera 10.5,chrome*/                varReduceright = Numbers.reduceright (function(Prev,cur,index,array) {returnPrev +cur;                                                }); Alert ("The Every method of array:" +ever); Alert ("The filter method of array:" +filter); Alert ("The map Method of array:" +map); Alert ("The Some method of array:" +some); Alert ("The Reduce method of array:" +reduce); Alert ("The Reduceright method of array:" +reduceright); }

Several built-in functions for array arrays in JavaScript

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.