Experience the powerful (insert, delete, or replace array elements) of splice () in js _ javascript skills

Source: Internet
Author: User
Javascriptsplice () is the most powerful, and can be used to insert, delete, or replace elements in an array. The following describes how to process arrays by interested friends. javascript splice () is the most powerful. It can be used to insert, delete, or replace array elements. Here is a one-to-one introduction!

1. Delete-used to delete an element. There are two parameters: the first parameter (the location of the first item to be deleted) and the second parameter (number of items to be deleted)
2. Insert-insert any element to the specified position of the array. Three parameters: the first parameter (actual location), the second parameter (0), and the third parameter (inserted item)
3. replace-insert any element to the specified position of the array, and delete any number of items, three parameters. The first parameter (start position), the second parameter (number of deleted items), and the third parameter (insert any number of items)

You can see the following code.

The Code is as follows:


Var lang = ["php", "java", "javascript"];
// Delete
Var removed = lang. splice (1, 1 );
Alert (lang); // php, javascript
Alert (removed); // java, return the deleted item
// Insert
Var insert = lang. splice (0th, "asp"); // insert data from locations
Alert (insert); // returns an empty array
Alert (lang); // asp, php, javascript
// Replace
Var replace = lang. splice (, "c #", "ruby"); // delete one item and insert two items
Alert (lang); // asp, c #, ruby
Alert (replace); // php, return the deleted item

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.