JavaScript methods Splice () and slice ()

Source: Internet
Author: User

1 Splice () 1.1 Description

The splice () method adds/deletes an item to/from the array, and then returns the item that was deleted. The method changes the original array . Link

1.2 Syntax
Arrayobject.splice (index,howmany,item1,....., ItemX)

Parameters

    • Index: Required. An integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array.
    • Howmany: Required. The number of items to delete. If set to 0, the item is not deleted.
    • Item1, ..., ItemX: Optional. Adds a new item to the array.

return value

    • Array: A new array containing the deleted items, if any.
1.3 Example
Add Item (function () {    var arr = [0, 1, 2, 3, 4];    Arr.splice (1, 0, 9, 10); Index location/delete quantity/optional, add item can be multiple    console.log (arr);//[0, 9, 10, 1, 2, 3, 4]} ());//delete item (function () {    var arr = [0, 1, 2, 3, 4];    Arr.splice (1, 2);    Console.log (arr); [0, 3, 4]} ());//delete and add items (function () {    var arr = [0, 1, 2, 3, 4];    Arr.splice (1, 2, 9, ten, one);    Console.log (arr); [0, 9, 10, 11, 3, 4]} ());
2 Slice () 2.1 Description

The slice () method returns the selected element from an existing array. Instead of modifying the array, the method returns a sub-array .

2.2 Syntax
Arrayobject.slice (Start,end)

Parameters

    • Start: Required. Specify where to start the selection. If it is a negative number, it specifies the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on.
    • End: Optional. Specifies where to end the selection. The parameter is the array subscript at the end of the array fragment. If this parameter is not specified, then the segmented array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the element starting from the end of the array.

return value

    • Returns a new array containing elements from start to end (excluding the element) from the Arrayobject.
2.3 Example
Intercept the middle segment (function () {    var arr = [0, 1, 2, 3, 4];    Arrs = Arr.slice (1, 3); Starting from index 1 to index 3, excluding item    Console.log (ARRS) of index 3,//[1, 2]} ()//intercept middle to last (function () {    var arr = [0, 1, 2, 3, 4];    Arrs = Arr.slice (1); End is empty, starting at index 1 to end    Console.log (ARRS);//[1, 2, 3, 4]} ());//Gets the last item (function () {    var arr = [0, 1, 2, 3, 4];    Arrs = Arr.slice (-1); -1 items to the last, 1 that is the last,-2 the penultimate    Console.log (ARRS);//[4]} ());//Exclude the last item (function () {    var arr = [0, 1, 2, 3, 4];
   
    arrs = Arr.slice (0,-1); First to 1, not including 1 (last)    Console.log (ARRS);//[0, 1, 2, 3]} ());
   

JavaScript methods Splice () and slice ()

JavaScript methods Splice () and slice ()

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.