


js array operations pop, push, unshift, splice, shift_javascript skills
May 16, 2016 pm 05:01 PM<script>
Array.prototype.pop=function( ){
if(this.length!=0)this.length--;
return this;
}
pop method
Removes the last element from an array and returns that element.
arrayObj.pop( )
The required arrayObj reference is an Array object.
Explanation
If the array is empty, undefined will be 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 of the array.
arrayObj.push([item1 [item2 [. . . [itemN ]]]])
Parameters
arrayObj
Required. An Array object.
item, item2,. . . itemN
Optional. The new element of this Array.
Description The
push method will add new 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 combine elements from 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 beginning of the array and returns the array.
arrayObj.unshift([item1[, item2 [, . . . [, itemN]]]])
Parameters
arrayObj
Required. An Array object.
item1, item2,. . .,itemN
Optional. The element to be inserted at the beginning of this Array.
Explanation The
unshift method inserts these elements into the beginning of an array, so these elements will appear in the array in the order of the parameter 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 方法
从一个数组中移除一个或多个元素,如果必要,在所移除元素的位置上插入新元素,返回所移除的元素。
arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])
参数
arrayObj
必选项。一个 Array 对象。
start
必选项。指定从数组中移除元素的开始位置,这个位置是从 0 开始计算的。
deleteCount
必选项。要移除的元素的个数。
item1, item2,. . .,itemN
必选项。要在所移除元素的位置上插入的新元素。
说明
splice 方法可以移除从 start 位置开始的指定个数的元素并插入新元素,从而修改 arrayObj。返回值是一个由所移除的元素组成的新 Array 对象。
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 方法
移除数组中的第一个元素并返回该元素。
arrayObj.shift( )
必选的 arrayObj 引用是一个 Array 对象。
说明
shift 方法可移除数组中的第一个元素并返回该元素。
var a=[1,2]
alert(a.shift())
alert(a)
</script>

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Recommended: Excellent JS open source face detection and recognition project

How to remove duplicate elements from PHP array using foreach loop?

PHP array key value flipping: Comparative performance analysis of different methods

PHP array multi-dimensional sorting practice: from simple to complex scenarios

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy

Application of PHP array grouping function in data sorting

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods

The role of PHP array grouping function in finding duplicate elements
