Slice () and splice () in JavaScript

Source: Internet
Author: User

Slice () and splice () are the Two methods of the prototype Array object in JavaScript, and because of the similarity of the method names, it is common to remember the roles of the two and make a clear distinction below.

1.slice (start[, end]) :

Takes an element from an array, returns the sub-array that is fetched, and has no effect on the original array . Wherethe start parameter is required to indicate the starting position of the element, the end parameter is optional, which represents the terminating position of the element, but does not contain the array[end] element (which can be understood as the element in the final fetched array has End-start ), if end is empty, the default is Array.lengh. See a few examples:

var numarray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]numarray.slice (5)//Return [5, 6, 7, 8, 9]numarray.slice (5, 6)//return [5]numarray/ /[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] The original array is unaffected


2.splice (start[, number, item1, item2, ..., itemn]) :

adds /deletes elements to an array , returns the deleted element, and affects the original array . Wherethe start parameter is required to indicate the starting position of the operation, and the number parameter is optional, indicating how many elements to delete,0 for not deleting the element;item1, item2, ..., itemn parameter is optional, indicating the element value to add. See a few examples:

var numarray = [0,1, 2, 3, 4, 5, 6, 7, 8, 9]myarray.splice (9,1)//[9]myarray//[0,, 3, 4, 5, 6, 7, 8]myarray.splice (8, 1, 8, 9, ten)//[8]myarray//[0, 3, 4, 5, 6, 7, 8, 9, 10]myarray.splice (1)//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]myarray//[ 0]

Add: common operations for arrays in JavaScript:

1. pop () Shift () :

1.1 Pop () deletes the last element of the array and returns the deleted element:

var numarray = [0,1, 2, 3, 4, 5, 6, 7, 8, 9]numarray.pop ()//9numarray//[0,1, 2, 3, 4, 5, 6, 7, 8]

1.2 shift () deletes the first element of the array and returns the element that was deleted:

var numarray = [0,1, 2, 3, 4, 5, 6, 7, 8, 9]numarray.shift ()//0numarray//[1,2, 3, 4, 5, 6, 7, 8, 9]

2. push () unshift () :

2.1 push () adds one or more elements to the end of the array and returns the length of the array after the element is added:

var numarray = [0,1, 2, 3, 4, 5, 6, 7, 8, 9]numarray.push (10,11,)//13numarray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

2.2 Unshift () adds one or more elements to the beginning of the array and returns the length of the array after the element is added:

var numarray = [0,1, 2, 3, 4, 5, 6, 7, 8, 9]numarray.unshift (10,11,)//13numarray = [10, 11, 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


This article is from the "barrel of fake dog excrement" blog, please be sure to keep this source http://xitongjiagoushi.blog.51cto.com/9975742/1653063

Slice () and splice () in JavaScript

Related Article

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.