JavaScript array iteration method _ javascript skills

Source: Internet
Author: User
This article mainly introduces the javaScript array iteration method in detail. ECMAScript5 defines five Iteration Methods for arrays, interested friends can refer to this article to introduce the javaScript array Iteration Method for your reference. The specific content is as follows:

Each method receives two parameters: the function to run on each item and the (optional) Scope object to run the function.

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

ForEach ()Run the given function for each item in the array. This method does not return values.
Every ()Run the given function for each item in the array. If each item in the array returns true, true is returned.
Some ()Run the given function for each item in the array. If any item in the array returns true, true is returned.
Fliter ()If true is returned for each item of the array, true is returned. Returns an array composed of true items.
Map ()If true is returned for each item of the array, true is returned. Returns an array composed of the results of each function call.

See the following example:

Var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; // every () 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, 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 () numbers. forEach (function (item, index, array) {alert (item) ;}); // multiple pop-up windows show the elements in the array respectively

Another javaScript Array iteration method is as follows:

Var arr = [3, 4, 5, 6, 7, "a"]; var isNum = function (elem, index, AAA) {return! IsNaN (elem);} var toUpperCase = function (elem) {return String. prototype. toUpperCase. apply (elem);} var print = function (elem, index) {console. log (index + ". "+ elem);}/* executes the test function for each item in the array until the item false is returned for the specified function is obtained. This method can be used to determine whether all items in the array meet a certain condition, similar to the meaning of & */var res = arr. every (isNum); console. log (res); // false;/* perform a test function on each item in the array until the item returns true. Use this method to determine whether all items in the array meet the conditions. similar to | */res = arr. some (isNum); console. log (res); // true/* execute a test function on each item in the array and construct a new array. the return value of true is added to the new array. If false is returned for an item, the new array does not contain this item */res = arr. filter (isNum); console. log (res); // [3, 4, 5, 6, 7]/* executes a function on each item in the array and constructs a new array, add the functions of each item in the original array to the new array. */Res = arr. map (toUpperCase); console. log (res); // ["3", "4", "5", "6", "7 ", "A"]/* executes A function on each item in the array, and does not return A value */res = arr. forEach (print); console. log (res); // self-expanding/* Array. prototype. every = function (fun, obj) {var len = this. length; if (typeof fun! = "Function") throw new TypeError (); for (var I = 0; I <len; I ++) {if (! Fun. call (obj, this [I], I, this) return false;} return true ;};*/

The above is all of the content in this article, hoping to help you learn the javaScript array iteration method.

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.