A summary of the compatibility of JavaScript array methods: IndexOf (), ForEach (), map (), filter (), some (), every ()

Source: Internet
Author: User
Tags array length lua javascript array

The array methods in the ECMA Script5 such as indexof (), ForEach (), map (), filter (), some () do not support ie6~8, but there is still a large majority of users in the country using Ie6~8, and the above array method is really useful. In the past, I would try not to use these methods for compatibility. But you can't lose a new one for the old one. Although jquery has been integrated with a lot of grammatical sugar, but jquery is too large, as a decent front end to know how to write the original compatibility. So these days, I began to ponder the compatibility of these methods. In fact, it is not difficult, is not confident before writing. After writing, the array is known more deeply. Summary see below.

Note: The following compatibility wording can be compatible to IE6;

IndexOf ()

The IndexOf () method returns the first index value found based on a given element, otherwise 1.

Grammar:

Array.indexof (searchelement[, fromIndex = 0])

Parameters:

Searchelement//elements in the array;

FromIndex//begins to find the index value of the specified element, the default value is 0 (that is, the entire array is searched for the specified element);

Fromindex is greater than or equal to the length of the array, stops finding and returns-1. If the index value provided in the argument is a negative number, it is offset at the end of the array, that is, 1 is the search starting from the last element, and 2 means finding it from the second-lowest element, and so on.

Compatibility: Incompatible ie6~8.

Write compatible ie6~8 According to the syntax of indexof () as follows:

Compatibility wording:
if (!ARRAY.PROTOTYPE.INDEXOF) {Array.prototype.indexOf =functionEle) {Get array lengthvar len =This.length;Check if the second parameter with a number is present and the default value is 0var fromIndex =Number (arguments[1]) | | 0; //when the second parameter is less than 0 o'clock, is the reverse lookup, which is equivalent to finding the index value for the index plus the array length after the value if (FromIndex < 0) {fromIndex + = Len;} //from FromIndex loop array while (FromIndex < len) {//checks if FromIndex exists and the corresponding array element is equal to Ele if (fromIndex in this && this[ FromIndex] = = = Ele) {return FromIndex;} fromindex++;} //when the array length is 0 o'clock returns a signal that does not exist: -1 if (len = = =  0) {return -1;}}       

Invocation Example:

var arr = ["a", "b", "c"];alert(arr.indexOf("b")); // 1
ForEach ()

The ForEach () method performs a given function once for each item in the array. The ForEach () method modifies the original array.

Grammar:

Array.foreach (callback[, Thisarg])

Parameters:

1. Callback: A function executed on each item of the array, receiving three parameters:
CurrentValue (the value of the current item), index (the current item's indexes), and array (the array itself).

2. Thisarg: Optional parameter. An object that is used as the value of this in the callback function, that is, the execution context of the callback function;

The ForEach method executes the callback function once for each item in the array that contains valid values in ascending order, those that have been deleted (using the Delete method, and so on), or items that have never been assigned are skipped (but do not include which values are undefined).

Compatibility: Incompatible ie6~8.

Write compatible ie6~8 According to the syntax of foreach () as follows:

Compatibility wording:
if (!Array.prototype.forEach) {Array.prototype.forEach =functionForEach (Callback) {Get array lengthvar len = this.length; if (typeof callback! =  "function") {throw new TypeError ();} //Thisarg is the execution context of the callback function var Thisarg = arguments[1]; for (var i = 0; i < Len; i++) {if (i in this) { //callback function receives three parameters: the value of the current item, the index of the current item, and the array itself Callback.call (Thisarg, this[i], I, this);} } }} 

Invocation Example:

var arr = ["a", "b", "c", "a", "d", "a"];arr.forEach(function(ele, index, array){ if(ele == "a") { array[index] = "**"; }});alert(newArr); // ["**", "b", "c", "**", "d", "**"]
Map ()

The map () method returns a new array that consists of a return value from each element in the original array that invokes a specified method.

Grammar:

Array.map (callback[, Thisarg])

Parameters:

1. Callback: A function executed on each item of the array, receiving three parameters:
CurrentValue (the value of the current item), index (the current item's indexes), and array (the array itself);

2. Thisarg: Optional parameter. An object that is used as the value of this in the callback function, that is, the execution context of the callback function;

Compatibility: Incompatible ie6~8.

Write compatible ie6~8 According to the syntax of map () as follows:

Compatibility wording:
if (!Array.prototype.map) {Array.prototype.map =functionCallback) {Get array lengthvar len =This.length;Iftypeof Callback! ="function") {throw New TypeError ();} //Create a new array of the same length as the original array to host the array elements modified by the callback function var newArr = new Array (LEN); //Thisarg is the execution context for the callback function var thisarg = arguments[1]; For (var i = 0; i < len; i++) { if (I  ) {Newarr[i] = Callback.call (thisarg, th Is[i], I, this ); }} return newArr;                }} 

Invocation Example:

var arr = ["a", "b", "c"];var newArr = arr.map(function(ele, index, array){ ele += "12"; return ele;});alert(newArr); // ["a12", "b12", "c12"]
Filter ()

The filter () method creates a new array with all the elements tested by the specified function and returns.

Grammar:

Arr.filter (callback[, Thisarg])

Parameters:

1. Callback: A function executed on each item of the array, receiving three parameters:
CurrentValue (the value of the current item), index (the current item's indexes), and array (the array itself);

2. Thisarg: Optional parameter. An object that is used as the value of this in the callback function, that is, the execution context of the callback function;

The filter invokes the callback function once for each element in the array and creates a new array with all elements that make callback return "true". Callback are only called on indexes that have already been assigned, and are not called for indexes that have been deleted or have never been assigned a value. Elements that are not tested by callback will only be skipped and will not be included in the new array. Filter does not change the original array.

Compatibility: Incompatible ie6~8.

Write compatible ie6~8 According to the syntax of the filter () as follows:

Compatibility wording:
if (!Array.prototype.filter) {Array.prototype.filter =functionCallback) {Get array lengthvar len =This.length;Iftypeof Callback! ="function") {throw new TypeError ();} //creates a new array that hosts the array elements modified by the callback function var newArr = array (); //Thisarg is the execution context of the callback function var thisarg = arguments[1]; for (var i = 0; i < Len; i++) {if (i in this) { if (Callback.call (Thisarg, this[i], I, this)) {Newarr.push (val);} }} return newArr;}           

Invocation Example:

var arr = [1, 2, 3, 4, 3, 2, 5];var newArr = arr.filter(function(ele, index, array){ if(ele < 3) { return true; }else { return false; }});alert(newArr); // [1, 2, 2]
Some ()

The Some () method tests whether certain elements in the array pass the test of the specified function. Returns a Boolean value. Some () does not change the array when it is invoked.

Grammar:

Arr.some (callback[, Thisarg])

Parameters:

1. Callback: A function executed on each item of the array, receiving three parameters:
CurrentValue (the value of the current item), index (the current item's indexes), and array (the array itself);

2. Thisarg: Optional parameter. An object that is used as the value of this in the callback function, that is, the execution context of the callback function;

Some () executes the callback function once for each element in the array until a value that causes callback to return a true is found. If such a value is found, some () returns TRUE. Otherwise, false is returned.

Compatibility: Incompatible ie6~8.

Write compatible ie6~8 According to the syntax of some () as follows:

Compatibility wording:
if (!Array.prototype.some) {Array.prototype.some =functionCallback) {Get array lengthvar len =This.length;Iftypeof Callback! = "function") { throw new TypeError ();} //Thisarg is the execution context for the callback function var thisarg = arguments[1]; for (var i = 0; i < len; i++) { if (I - in this && callback.call (Thisarg, this[i] , I, this )) { return true;}} return false;}}                

Invocation Example:

var arr = [1, 2, 3, 4, 3, 2, 5];var newArr = arr.some(function(ele, index, array){ if(ele < 2) { return true; }else { return false; }});alert(newArr); // true
Every ()

The Every () method tests whether all elements of the array pass the test of the specified function. Every () does not change the original array.

Grammar:

Arr.every (callback[, Thisarg])

Parameters:

1. Callback: A function executed on each item of the array, receiving three parameters:
CurrentValue (the value of the current item), index (the current item's indexes), and array (the array itself);

2. Thisarg: Optional parameter. An object that is used as the value of this in the callback function, that is, the execution context of the callback function;

The Every () method executes the callback function once for each element in the array. Returns true only if all elements return true in the callback function, otherwise false is returned.

Compatibility: Incompatible ie6~8.

Write compatible ie6~8 According to the syntax of some () as follows:

Compatibility wording:
if (!Array.prototype.every) {Array.prototype.every =functionCallback) {Get array lengthvar len =This.length;Iftypeof Callback! = "function") { throw new TypeError ();} //Thisarg is the execution context for the callback function var thisarg = arguments[1]; for (var i = 0; i < len; i++) { if (I - in this &&!callback.call (Thisarg, this[i ], I, this ) { return false;}} return true;}}                 

Invocation Example:

var arr = [ 1, 2, 3,  4, 3, 2, 5]; var newArr = arr.every (function ( Ele, index, array) {if (Ele < 3) { return true;} else {return false;}); alert (NEWARR); //false 


This article son Carpenter _zijor, reprint please specify source: http://www.dengzhr.com/js/362


A summary of the compatibility of the

JavaScript array methods: IndexOf (), ForEach (), map (), filter (), some (), every ()

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.