The four common methods of ECMA array prototyping and its compatible interpretation

Source: Internet
Author: User

I. Overview

The Array.prototype.some method tests whether certain elements in the array pass the test of the specified function. False by returning True otherwise

The Array.prototype.forEach method performs a given function once for each item in the array. No return

Array.prototype.map each element in the original array invokes a new array that consists of the return value after the specified method. Returns a new array with the same length as the original array

Array.prototype.filter tests all the elements with the specified function and creates a new array that contains all the elements passed by the test. Returns a new array returned by the tested element

Two. Syntax

var array = []

array. Map (callback[, Thisarg]);

function callback (Currentvalue,index,array) {}

Three. Parameters

callbackThe elements in the original array return a new element after the method.

currentValue
callbackThe first parameter of the array in which the element is currently passed.
index
callbackThe second parameter, the index of the element that is currently being passed in the array.
array
callback The third argument, called map the array of methods.

thisArgcallback this The object to point to when the function is executed.

Four. Usage 1.some usage

Finds if an array has elements greater than 10, finds immediate termination, so fewer loops

functionIsbigenough(element, index, array){Return(element>=10);}var passed=[2,5,8,1,4] some (Isbigenough//passed is falsepassed = [12 5814 ]. Some//passed is true           

 

2.foreach usage

The following code outputs a row of records for each array element:

functionLogarrayelements(element, index, array){Console.Log("A["+ index + + element [259< Span class= "token punctuation" >]. Foreach//logs: //a[0] = 2 //a[1] = 5 //a[2] = 9 

3.map method

The following code converts all the words in an array to the corresponding plural form.

functionFuzzyplural(Single){var result= Single.Replace(/o/g,E);If(Single===' Kangaroo '){Result+=' SE ';}return result;}var words = [ "foot"  "Goose"  "Moose" ];console. Log (Words. Map; ["Feet", "geese", "Meese", "Kangareese"]           
< Span class= "token operator" >   
4.filter method

The following example uses the filter creation of a new array whose elements consist of elements that have values greater than 10 in the original array.

functionIsbigenough(element){return element >= 10}var filtered = [12 5813044< Span class= "token punctuation" >]. Filter//filtered is [[+],]          
< Span class= "token operator" > five. Interpretation of compatibility and its processing methods

Not much else, IE9.

Then we have to add a method to him, the foreigner wrote so concise have to admire. Let me read the first one for you.

Need knowledge call usage in usage

1.some method

if (! Array.prototype.some) Array.prototype.some = function (i, m) {//If the some method is not present on the prototype, add the some method and And some receive two parameters
var f = this.length; Because it's an array instance call, so here's F for calling his array length
if ("function"! = typeof i) throw new T Ypeerror; Limit the parameters of I to functions, which is called callback functions, otherwise the error
for (var j = 0, J < F, J + +) if (j in this && I.call (M, This[j], J, this)) return! 0; J is subscript J in this is the test array under which the subscript has a value to perform the next round loop

Return! 1; If there is a corresponding value,&& the left is true, you can execute the right, and call the callback method with M
}; and pass three parameters This[j] is the current element, J is subscript, this is the array

If the internal is false then the next round of the loop, until the end of return false not found, if again I method return true, then some returned really found

2. TheForEach method

if (! Array.prototype.forEach) Array.prototype.forEach = function (i, m) {
var f = this.length;
if ("function"! = typeof i) throw new TypeError;
for (var j = 0, J < F, J + +) J in this && I.call (M, This[j], J, this)
};
3.map method
if (! ARRAY.PROTOTYPE.MAP) Array.prototype.map = function (i, m) {
var f = this.length;
if ("function"! = typeof i) throw new TypeError;
for (var j = Array (f), n = 0; n < f; n++) n in this && (j[n] = I.call (M, This[n], N, this));
Return J
};
4.filler method
if (! Array.prototype.filter) Array.prototype.filter = function (i, m) {
var f = this.length;
if ("function"! = typeof i) throw new TypeError;
for (var j = [], n = 0; n < F; n++) if (n on this) {
var u = this[n];
I.call (M, u, N, this) && J.push (U)
}
Return J
};

The four common methods of ECMA array prototyping and its compatible interpretation

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.