Copy code code as follows:
<script>
Array.prototype.pop = Function () {
if (this.length! = 0) this.Length-;
Return this;
}
POP method
Remove the last element in the array and return the element.
arrayObj.pop ()
The necessary ArrayObj reference is an array object.
illustrate
If the array is empty, it will return to undefined.
Copy code code as follows:
var a = [1,2,3,4]
a.pop ()
alert (a)
alert (a.pop ()) </script> <script>
push method
Add new elements to a array and return the new length value of the array.
arrayObj.push
parameter
arrayObj
Must be options. An array object.
item, item2 ,...... Itemn
Options. The new element of Array.
illustrate
The push method will add these elements in the order of new elements. If one of the parameters is an array, the array will be added to the array as a single element. If you want to merge two or more elements in the array, use the Concat method.
Copy code code as follows:
Array.prototype.push = Function () {
var len = arguments.length;
if (len> 0) for (var I = 0; i <len; i ++) this [this.Length] = arguments [i];
Return this.Length;
}
var a = [1,2,3,4]
A.push (5)
alert (a)
alert (a.push (6)) </script> <script>
UNSHIFT method
Insert the specified element into the array position and return to the array.
arrayObj.unshift ([item1 [, item2 [,... [, ites]]]))
parameter
arrayObj
Must be options. An array object.
item1, item2 ,....., it Itemn
Options. Insert the element of the starting part of the Array.
illustrate
The UNSHIFT method inserts these elements into the beginning of an array, so these elements will appear in the array in the sequence in the parameter sequence.
Copy code code as follows:
Array.prototype.unshift = Function () {
var len = arguments.length;
this.reverse ();
if (len> 0) for (var I = len; i> 0; i-) this [this.length] = arguments [i-1];
Return this.reverse ();
}
var a = [1,2,3,4]
a.unshift ()
alert (a)
a.unshift (5,6)
alert (a)
Alert (a.unshift (7)) </script> <script language = "jscript">
Array.prototype.splice = Function () {
var len = arguments.length;
var tarray = [];
if (len> 1)
for (var I = arguments [0]+arguments [1]; I <THIS.Length; I ++) tarray [tarray.length] = this [i];
this.Length = Arguments [0];
if (len> 2) for (var I = 2; I <len; i ++) this [this.length] = arguments [i];
var tlen = tarray.length;
for (var I = 0; I <tlen; i ++) this [this.Length] = tarray [i];
}
Return this;
}
var a = [1,2,3,4];
Splice method
Remove one or more elements from one array. If necessary, insert new elements on the position of the removal element and return the removed element.
arrayObj.Splice (Start, Deletecount, [item1 [, item2 [,... [, itemn]]]))
parameter
arrayObj
Must be options. An array object.
start
Must be options. Specify the starting position of removing elements from the array, this position is calculated from 0.
deletecount
Must be options. The number of elements to be removed.
item1, item2 ,....., it Itemn
Must be options. New elements inserted in the position of the removal element.
illustrate
The Splice method can remove the specified number of elements starting from the Start position and insert new elements to modify ArrayObj. The return value is a new Array object composed of removed elements.
Copy code code as follows:
Alert (A.Splice (0,1));
Alert (A.Splice (0,1,1,1,1,1,1,1,1,1)))
</script> <script>
Array.prototype.shift = Function () {
var f = this [0];
for (var I = 0; I <THIS.Length; I ++) this [i] = this [i+1];
this.Length-;
Return f;
}
shift method
Remove the first element in the array and return the element.
arrayObj.shift ()
The necessary ArrayObj reference is an array object.
illustrate
The shift method can remove the first element in the array and return the element.
Copy code code as follows:
var a = [1,2]
alert (a.shift ()))
alert (a)
</script>