Pop,push,unshift,splice,shift of JS Array operation

Source: Internet
Author: User
Tags arrays

  This article mainly introduces the pop,push,unshift,splice,shift of JS array operation. Need friends can come to the reference, I hope to help you.

  Code as follows: <script>     array.prototype.pop=function () {         IF ( this.length!=0) this.length--;          return this;         &NBSP}       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.   Code as follows: Var a=[1,2,3,4]     A.pop ()     alert (a)     alert (A.pop ()) </script><s cript>       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      available option. 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.      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;         &nbsp,     var a=[1,2,3,4]     A.push (5)     alert (a)   &N Bsp 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. The element that will be inserted at the beginning of the Array.      Description    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.    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 ();         &nbsp,     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]+arg uments[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, and returns the removed element.      Arrayobj.splice (Start, DeleteCount, [item1[, item2[, ...) [, Itemn]]]      Parameters    arrayobj      required option. An Array object.      Start      required options. 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    Splice method can modify 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.      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;         &NBSP,         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.      Code as follows: Var a=[1,2]     alert (A.shift ())     alert (a)     </script> & nbsp    

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.