Detailed JavaScript Splice () method __java

Source: Internet
Author: User

The splice () method in JavaScript is a strong array method that has many uses.
The main purpose of splice () is to insert items into the middle of the array.

There are 3 different ways:
Delete-You can delete any number of items by specifying only 2 parameters: the position of the first item to delete and the number of items to delete.
For example, splice (0,2) deletes the first two items in the array.
Insert--You can insert any number of items to a specified location, and you only need to provide 3 parameters: Knight position, 0 (number of items to delete), and items to insert.
If you want to insert more than one item, you can pass in the four, five, and always any number of items.
For example, splice (2,1, "Red", "green") deletes the entry for the current array position 2, and then inserts the string "Red" and "green" from position 2.
Replace--You can point to any number of items inserted at the specified location and delete any number of items at the same time, specifying only 3 specified parameters: Start position, number of items to delete, and any number of items to insert.
The image inserted is not necessarily equal to the number of items deleted. For example, splice (2,2, "Red", "green") deletes the entry for the current array position 2, and then inserts the string "Red" and "green" from position 2.
The splice () method always returns an array that contains items that are removed from the element array (if no items are deleted, an empty array is returned)


<script>
var colors = ["Red", "green", "blue"];
var removed = Colors.splice (0,1); Delete the first item
alert (colors); Green,blue
alert (removed); Red, returns an array of values containing an item

removed = Colors.splice (1, 0, "yellow", "orange"); Insert two items starting at position 1
alert (colors); Green,yellow,organge,blue
alert (removed); Returns an empty array

removed = Colors.splice (1, 1, "Red", "purple"); Insert two items to delete an item
alert (colors); Green,red,purple,orange,blue
alert (remove); Yellow, the returned array contains only one item
</script>

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.