Js array of pop operation a push unshift splice shift
- 2020-03-30 01:37:08
- OfStack
<script>
Array.prototype.pop=function(){
if(this.length!=0)this.length--;
return this;
}
The pop method
Removes the last element in the array and returns that element.
ArrayObj. Pop ()
The required arrayObj reference is an Array object.
Description
If the array is empty, undefined is returned.
var a=[1,2,3,4]
a.pop()
alert(a)
alert(a.pop())</script><script>
Push method
Adds a new element to an array and returns the new length value of the array.
ArrayObj. Push ([item1 [item2 [... [itemN]]]])
Parameter
ArrayObj
Will be options. An Array object.
Item, item2,.. itemn&item;
Optional. A new element of the Array.
Description
The push method adds the elements in the order in which they appear. If one of the arguments is an array, the array is added to the array as a single element. If you want to merge elements in two or more arrays, use the concat method.
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
Inserts the specified element into the starting position of the array and returns the array.
ArrayObj. Unshift ([item1 [, item2 [... [, itemN]]]])
Parameter
ArrayObj
Will be options. An Array object.
Item1, item2,...., itemN
Optional. The element that will be inserted into the beginning of the Array.
Description
The unshift method inserts these elements into the beginning of an array, so they appear in the array in the order of the argument sequence.
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
Removes one or more elements from an array, inserts a new element where the removed element is, if necessary, and returns the removed element.
Arrayobj. splice(start, deleteCount, [item1[, item2[,..
Parameter
ArrayObj
Will be options. An Array object.
Start
Will be options. Specifies the starting position for removing an element from an array, which is evaluated from 0.
DeleteCount
Will be options. The number of elements to remove.
Item1, item2,...., itemN
Will be options. The new element to be inserted where the removed element is.
Description
The splice method modifies arrayObj by removing a specified number of elements from the start position and inserting new elements. The return value is a new Array object composed of the removed elements.
alert(a.splice(0,1));
alert(a.splice(0,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;
}
The shift method
Removes the first element in the array and returns that element.
ArrayObj. The shift ()
The required arrayObj reference is an Array object.
Description
The shift method removes the first element in an array and returns it.
var a=[1,2]
alert(a.shift())
alert(a)
</script>