The technique of array _javascript of Chrome native method

Source: Internet
Author: User
Let's look at some of the array methods implemented by CHROME/15
---------------------------------------------------------------------------------------------
Concat: This said, the only thing to note is that concat is not in-place modification, refers to the return of the link after the result, the other point is to expand the first layer of the array
Join: Connecting arrays
Pop: Out of stack operation, note that this is also in place to modify the original array
Push: In-stack operation, note that this is also in place to modify the original array
Reverse: Reverse array, note that this is also an in-place modification of the original array
Shift: Out of the team, note that this is also in place to modify the original array
Unshift: Inserts an item at the head of the array, followed by the subsequent move
Slice: A common operation in this way is to transform an array of classes into a true array by intercepting the part of an array.
Splice: Modify the array, you can insert the new item, note that this is also in place to modify the original array
Sort: Array ordering, note that this is also an in-place modification of the original array
toLocaleString: Returns the local string form of an array, typically with a comma
ToString: Returns the string form of an array, typically with a comma
---------------------------------------------------------------------------------------------
IsArray: To determine if a variable is not an array, note that this is a static method that invokes the form Array.isarray ()
---------------------------------------------------------------------------------------------
Every: To determine if all the items in an array satisfy the condition, return true if all conditions are met, or false
Some: This can be associated with every, every requirements are all true last is True,some if one is true, the return is true
Filter: Filters out eligible items from an array according to a given condition, and returns as a new array, otherwise null
ForEach: Perform the given action once for each item in the array
IndexOf: Returns the first position of the given item in the array (starting with the 0 subscript)
LastIndexOf: Contrary to the indexof
Map: Each item in an array performs the given action at once and returns the modified array
Reduce:reduce (Func,init) func is a two-dollar function that will func the elements of a SEQ sequence, each carrying a pair of elements of the previous result and the next sequence, and continuously effect the existing results and the next value on the resulting subsequent result. Finally, we reduce our sequence to a single return value.
Implementation of Reduceright:reduce from right to left
---------------------------------------------------------------------------------------------
When we implement our own small library or some tools, we can extend it a little bit
Copy Code code as follows:

Object.prototype.extend = function (src) {
for (var i in Src) {
This[i] = Src[i];
}
}
Array.extend ({
Toarray:function (arraylike) {
try{
return [].slice.call (arraylike);
}catch (ex) {
var ret = [];
for (var i = 0,len = Arraylike.length i < len; i++) {
Ret.push (Arraylike[i]);
}
}
},
IsArray: (Array.isarray)? Array.isArray:function (ele) {
return ele.constructor = = Array;
}
})
Array.prototype.extend (function () {
var each = Array.prototype.forEach | | function (fn,obj) {
for (var i = 0,len = This.length i < len; i++) {
Fn.call (Obj,this[i]);
}
};
var filter = Array.prototype.filter | | function (fn,obj) {
var result = [];
for (var i = 0,len = This.length i < len; i++) {
if (Fn.call (Obj,this[i])) {
Result.push (This[i]);
}
}
return result;
};
var every = Array.prototype.every | | function (fn,obj) {
for (var i = 0,len = This.length i < len; i++) {
if (!fn.call (Obj,this[i])) {
return false;
}
}
return true;
};
var some = Array.prototype.some | | function (fn,obj) {
for (var i = 0,len = This.length i < len; i++) {
if (Fn.call (Obj,this[i])) {
return true;
}
}
return false;
};
var indexOf = Array.prototype.indexOf | | function (dest) {
for (var i = 0; i < this.length; i++) {
if (dest = = This[i]) {
return i;
}
}
return-1;
};
var map = Array.prototype.map | | function (fn,obj) {
var result = [];
for (var i = 0,len = This.length i < len; i++) {
Result.push (Fn.call (obj,this[i));
}
return result;
};
var Reduce =array.prototype.reduce | | function (Fn,init) {
var result = Init | | This[0];
for (var i = 0; i < this.length; i++) {
result = FN (result,this[i]);
}
return result;
}
return {
Map:map,
Each:each,
Some:some,
Every:every,
Filter:filter,
Indexof:indexof,
Reduce:reduce
}
})())

Here is a small example, to specify the UL below the Li set content:
Copy Code code as follows:

<ul id= "Test" >
<li></li>
<li></li>
<li></li>
</ul>
<script type= "Text/javascript" >
Array.prototype.slice.call (document.getElementById (' Test '). getElementsByTagName (' Li '), 0). Each (the function (LI) {
li.innerhtml = ' Test each ';
})
</script>

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.