JavaScript Array Extension implementation code

Source: Internet
Author: User
Tags javascript array

IndexOf
Returns the index of the element in the array. If no index exists,-1 is returned. Similar to the indexOf method of string.
If other browsers do not implement this method, use the following code for compatibility:
Copy codeThe Code is as follows:
Array. prototype. indexOf = function (el, start ){
Var start = start | 0;
For (var I = 0; I <this. length; ++ I ){
If (this [I] === el ){
Return I;
}
}
Return-1;
};
Var array = [2, 5, 9];
Var index = array. indexOf (2 );
// Index is 0
Index = array. indexOf (7 );
// Index is-1

LastIndexOf
Similar to the lastIndexOf method of string.
If other browsers do not implement this method, use the following code for compatibility:
Copy codeThe Code is as follows:
Array. prototype. indexOf = function (el, start ){
Var start = start | 0;
For (var I = 0; I <this. length; ++ I ){
If (this [I] === el ){
Return I;
}
}
Return-1;
};

ForEach
Similar each methods are implemented in various libraries.
If other browsers do not implement this method, use the following code for compatibility:
Copy codeThe Code is as follows:
Array. prototype. forEach = function (fn, thisObj ){
Var scope = thisObj | window;
For (var I = 0, j = this. length; I <j; ++ I ){
Fn. call (scope, this [I], I, this );
}
};
Function printElt (element, index, array ){
Print ("[" + index + "] is" + element); // assumes print is already defined
}
[2, 5, 9]. forEach (printElt );
// Prints:
// [0] is 2
// [1] is 5
// [2] is 9

Every
If each element in the array can pass the test of the given function, true is returned, whereas false is returned. In other words, the given function must also return true and false.
If other browsers do not implement this method, use the following code for compatibility:
Copy codeThe Code is as follows:
Array. prototype. every = function (fn, thisObj ){
Var scope = thisObj | window;
For (var I = 0, j = this. length; I <j; ++ I ){
If (! Fn. call (scope, this [I], I, this )){
Return false;
}
}
Return true;
};
Function isBigEnough (element, index, array ){
Return (element <= 10 );
}
Var passed = [12, 5, 8,130, 44]. every (isBigEnough );
// Passed is false
Passed = [12, 54, 18,130, 44]. every (isBigEnough );
// Passed is true

Some
Similar to the every function, but returns true if one test passes the given function.
If other browsers do not implement this method, use the following code for compatibility:
Copy codeThe Code is as follows:
Array. prototype. some = function (fn, thisObj ){
Var scope = thisObj | window;
For (var I = 0, j = this. length; I <j; ++ I ){
If (fn. call (scope, this [I], I, this )){
Return true;
}
}
Return false;
};
Function isBigEnough (element, index, array ){
Return (element> = 10 );
}
Var passed = [2, 5, 8, 1, 4]. some (isBigEnough );
// Passed is false
Passed = [12, 5, 8, 1, 4]. some (isBigEnough );
// Passed is true

Filter
Place the elements that meet the conditions in a new array and return them.
If other browsers do not implement this method, use the following code for compatibility:
Copy codeThe Code is as follows:
Array. prototype. filter = function (fn, thisObj ){
Var scope = thisObj | window;
Var a = [];
For (var I = 0, j = this. length; I <j; ++ I ){
If (! Fn. call (scope, this [I], I, this )){
Continue;
}
A. push (this [I]);
}
Return;
};
Function isBigEnough (element, index, array ){
Return (element <= 10 );
}
Var filtered = [12, 5, 8,130, 44]. filter (isBigEnough );

Map
Let each element in the array call the given function, and then put the result into the new array to return ..
If other browsers do not implement this method, use the following code for compatibility:
Copy codeThe Code is as follows:
Array. prototype. map = function (fn, thisObj ){
Var scope = thisObj | window;
Var a = [];
For (var I = 0, j = this. length; I <j; ++ I ){
A. push (fn. call (scope, this [I], I, this ));
}
Return;
};
<Div id = "highlighter_240589">
Var numbers = [1, 4, 9];
Var roots = numbers. map (Math. sqrt );
// Roots is now [1, 2, 3]
// Numbers is still [1, 4, 9]

Reduce
Let the array element call the given function in sequence and return a value. In other words, the returned value must be used for the given function.
If other browsers do not implement this method, use the following code for compatibility:
Copy codeThe Code is as follows:
Array. prototype. reduce = function (fun/*, initial */)
{
Var len = this. length >>> 0;
If (typeof fun! = "Function ")
Throw new TypeError ();
If (len = 0 & arguments. length = 1)
Throw new TypeError ();
Var I = 0;
If (arguments. length> = 2 ){
Var rv = arguments [1];
} Else {
Do {
If (I in this ){
Rv = this [I ++];
Break;
}
If (++ I> = len)
Throw new TypeError ();
} While (true );
}

For (; I <len; I ++ ){
If (I in this)
Rv = fun. call (null, rv, this [I], I, this );
}
Return rv;
};

Copy codeThe Code is as follows:
Var total = [0, 1, 2, 3]. reduce (function (a, B) {return a + B ;});
// Total = 6

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.