Pop,push,unshift,splice,shift_javascript Techniques of JS array operation

Source: Internet
Author: User
Copy Code code as follows:

<script>
Array.prototype.pop=function () {
if (this.length!=0) this.length--;
return this;
}

Pop method
Moves the last element in the divisor group and returns the element.

Arrayobj.pop ()

The required arrayobj reference is an Array object.

Description
If the array is empty, then the undefined is returned.
Copy Code code as follows:

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]]])

Parameters
Arrayobj

Required option. An Array object.

Item, Item2,... itemn

Options available. The new element of the Array.

Description
The push method adds these elements in the order in which the new elements appear. If one of the arguments is an array, the array is added as a single element to the arrays. If you want to merge elements from two or more arrays, 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
Inserts the specified element into the starting position of the array and returns the array.

Arrayobj.unshift ([item1[, item2 [, ...] [, Itemn]]]

Parameters
Arrayobj

Required option. An Array object.

Item1, item2,.. ., itemn

Options available. The element that will be inserted at the beginning of the Array.

Description
The Unshift method inserts these elements into the beginning of an array, so these elements appear in the order in the sequence of arguments in the array.
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
Removes one or more elements from an array and, if necessary, inserts a new element at the position of the removed element, returning the removed element.

Arrayobj.splice (Start, DeleteCount, [item1[, item2[, ...) [, Itemn]]]

Parameters
Arrayobj

Required option. An Array object.

Start

Required option. Specifies that the starting position of the element is removed from the array, which is calculated starting at 0.

DeleteCount

Required option. The number of elements to remove.

Item1, item2,.. ., itemn

Required option. The new element to insert at the location of the removed element.

Description
The splice method modifies arrayobj by removing the specified number of elements starting from the start position and inserting new elements. The return value is a new Array object that consists of the removed element.
Copy Code code as follows:

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;
}

Shift Method
Moves the first element in the divisor group and returns the element.

Arrayobj.shift ()

The required arrayobj reference is an Array object.

Description
The shift method moves the first element in the divisor group and returns the element.
Copy Code code as follows:

var a=[1,2]
Alert (A.shift ())
Alert (a)
</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.