Home > Web Front-end > JS Tutorial > js array operations pop, push, unshift, splice, shift_javascript skills

js array operations pop, push, unshift, splice, shift_javascript skills

WBOY
Release: 2016-05-16 17:01:52
Original
1477 people have browsed it
Copy code The code is as follows:

<script> <br>Array.prototype.pop=function( ){ <br> if(this.length!=0)this.length--; <br> return this; <br> } <br> </div> <br><strong>pop method <br></strong> Removes the last element from an array and returns that element. <br><br>arrayObj.pop( ) <br><br>The required arrayObj reference is an Array object. <br><br>Explanation <br>If the array is empty, undefined will be returned. <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="32422" class="copybut" id="copybut32422" onclick="doCopy('code32422')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code32422"> <br>var a=[1,2,3,4] <br>a.pop() <br>alert(a) <br>alert(a.pop())</script><script> <br> </div> <br><strong>push method</strong> <br>Adds a new element to an array and returns the new length of the array. <br><br>arrayObj.push([item1 [item2 [. . . [itemN ]]]]) <br><br>Parameters <br>arrayObj <br><br>Required. An Array object. <br><br>item, item2,. . . itemN <br><br>Optional. The new element of this Array. <br><br> Description The <br>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. <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="10396" class="copybut" id="copybut10396" onclick="doCopy('code10396')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code10396"> <br>Array.prototype.push=function(){ <br> var len=arguments.length; <br> if(len>0)for(var i=0;i<len;i )this[this.length]=arguments[i]; <BR> return this.length; <BR> } <BR>var a=[1,2,3,4] <BR>a.push(5) <BR>alert(a) <BR>alert(a.push(6))</ script><script> <br> </div> <br><strong>unshift method </strong> <br>Inserts the specified element into the beginning of the array and returns the array. <br><br>arrayObj.unshift([item1[, item2 [, . . . [, itemN]]]]) <br><br>Parameters <br>arrayObj <br><br>Required. An Array object. <br><br>item1, item2,. . .,itemN <br><br>Optional. The element to be inserted at the beginning of this Array. <br><br>Explanation The <br>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. <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="49917" class="copybut" id="copybut49917" onclick="doCopy('code49917')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code49917"> <br>Array.prototype.unshift=function(){    <br>     var len=arguments.length;    <br>     this.reverse();    <br>     if(len>0)for(var i=len;i>0;i--)this[this.length]=arguments[i-1];    <br>     return this.reverse();    <br>     }    <br>var a=[1,2,3,4]    <br>a.unshift()    <br>alert(a)    <br>a.unshift(5,6)    <br>alert(a)    <br>alert(a.unshift(7))</script><script>    <br>Array.prototype.shift=function(){    <br>     var f=this[0];    <br>     for(var i=0;i<this.length;i )this[i]=this[i 1];    <br>     this.length--;    <br>     return f;    <br>     }    <br> </div>   <br><strong>shift 方法</strong>   <br>移除数组中的第一个元素并返回该元素。   <br><br>arrayObj.shift( )   <br><br>必选的 arrayObj 引用是一个 Array 对象。   <br><br>说明   <br>shift 方法可移除数组中的第一个元素并返回该元素。   <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="13600" class="copybut" id="copybut13600" onclick="doCopy('code13600')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code13600"> <br>var a=[1,2]    <br>alert(a.shift())    <br>alert(a)    <br></script>   
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template