JavaScript array Extension implements code _javascript techniques

Source: Internet
Author: User
Tags javascript array
indexOf
Returns the index of the element in the array, and returns-1. Similar to the IndexOf method of string.
If other browsers do not implement this method, you can implement compatibility with the following code:
Copy Code code 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, you can implement compatibility with the following code:
Copy Code code 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 method is implemented in all types of libraries.
If other browsers do not implement this method, you can implement compatibility with the following code:
Copy Code code 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
Returns true if each element in the array can pass the test of the given function, false instead. In other words, the given function must also return true and false.
If other browsers do not implement this method, you can implement compatibility with the following code:
Copy Code code 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 = [5, 8, 130, 44].every (Isbigenough);
Passed is false
Passed = [A, Si, 130, 44].every (Isbigenough);
Passed is true

some
Similar to the Every function, but returns true whenever there is a test that passes a given function.
If other browsers do not implement this method, you can implement compatibility with the following code:
Copy Code code 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 = [5, 8, 1, 4].some (Isbigenough);
Passed is true

Filter
The element that matches the condition is returned in a new array.
If other browsers do not implement this method, you can implement compatibility with the following code:
Copy Code code 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 A;
};
function Isbigenough (element, index, array) {
Return (element <= 10);
}
var filtered = [5, 8, 130, 44].filter (Isbigenough);

Map
Let each element in the array call the given function, and then put the resulting results back in the new array ...
If other browsers do not implement this method, you can implement compatibility with the following code:
Copy Code code 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 A;
};
<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 sequentially, and finally return a value, in other words, the given function must use the return value.
If other browsers do not implement this method, you can implement compatibility with the following code:
Copy Code code 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 Code code as follows:

var total = [0, 1, 2, 3].reduce (function (A, b) {return a + B;});
Total = = 6

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.