Array Splice method

Source: Internet
Author: User

JS Array Splice method

function

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

1. Add

Let arr = [n/a];

Console.log (arr); //[1, 2, 3]
Array.prototype.splice.call (arr,1,0, ' 4 ');
Console.log (arr); //[1, "4", 2, 3]

/**

* Array.prototype.splice.call (arr,1,0, ' 4 ');

* Call after Arr is an array that points to arr,1 is the position of the ARR array where the 1 (second) element is inserted into an element, 0 means not removing other elements, ' 4 ' is the inserted element;

* /

2. Modifications

Let arr = [n/a];

Console.log (arr);//[1, 2, 3]
Array.prototype.splice.call (arr,1,1, ' 4 ');
Console.log (arr);//[1, "4", 3]

/**

* Array.prototype.splice.call (arr,1,1, ' 4 ');

* The arr behind the call is the array that points to arr, the first 1 is the position of the 1 (second) element in the ARR array, and the second 1 is the element that is inserted at the position of the subscript 1, and the ' 4 ' is the elements being injected;

*/

3. Delete

Let arr = [n/a];

Console.log (arr);//[1, 2, 3]
Array.prototype.splice.call (arr,1,1);
Console.log (arr);//[1, 3]

/**

* Array.prototype.splice.call (arr,1,1);

* The arr behind the call is an array to arr, the first 1 is the position of the 1 (second) element in the ARR array, and the second 1 is the deletion of an element at the position of the subscript 1;

*/

Array Splice method

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.