JavaScript Array iterative method _javascript skills

Source: Internet
Author: User
Tags arrays javascript array

This article introduces the JavaScript array iterative method for everyone to refer to, the specific contents are as follows

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

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

Each item in the ForEach () array runs the given function. The method does not have a return value.
every () runs the given function for each item in the array, and returns true if each entry of the list returns true.
some () each item in the array runs the given function, and returns True if any one of the arrays returns true.
fliter () returns True if each item of the array returns TRUE. Returns an array of items that the function returns True.
map () returns True if each item of the array returns TRUE. Returns an array of the results of each function call.

Take a look at the following examples:

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,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 ()
Numbers.foreach (function (item, index, array) {
 alert (item);
Multiple window displays the elements in the array, respectively.

Another JavaScript array iteration method, 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); /* * The test function is executed for each item in the array until the item that returns false to the specified function is obtained.
Use this method to determine whether all items in an array meet a condition, similar to the meaning of && var res = arr.every (isnum);

Console.log (res);//false; /* The test function is executed for each item in the array until the item that returns True is obtained. Use this method to determine whether all items in the array meet the criteria. Similar to | |
The meaning/Res = arr.some (isnum); Console.log (res);//true/* Performs a test function on each item in the array, constructs a new array, and the item that returns True is added to the new array.
If an item returns false, the new array will not contain this item */RES = Arr.filter (isnum); Console.log (res);//[3, 4, 5, 6, 7]/* Each item in the array executes the function and constructs a new array and adds the function knot of each item in the original to the new array.
* res = ARR.MAP (touppercase);
Console.log (res);//["3", "4", "5", "6", "7", "A"]/* Every item in the array is executed, not returning the value */res = Arr.foreach (print);

 

 

Console.log (RES); Self extension/*array.prototype.every = function (fun,obj) {var len = this.length; if (typeof fun!= "function") throw new Typee
Rror (); for (var i = 0; i < len; i++) {if (!fun). Call (Obj,this[i], i,this)) return false;
return true;
 };*/

The above is all in this article, and I want to help you learn how to iterate over JavaScript arrays.

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.