[Js] To delete an array of specified elements

Source: Internet
Author: User

Write in front

In recent projects, useful to the JS array of operations, before you almost no use of this method, here to record, is a summary of what is learned.

Array Object Splice method

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.

Basic syntax

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

Parameter description

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: Optional. Adds a new item to the array.

return value

Array: The new array after the element is deleted.

Description

The splice () method deletes 0 or more elements starting at index and replaces the deleted elements with one or more values declared in the argument list.

If the element is removed from the Arrayobject, the array containing the deleted element is returned.

Array Splice Method The first argument is the starting position, and the second parameter is the number to delete.

<script>
var arr = [1, 2, ' A ', ' B '];
Console.log (' original array ');
Console.log (Arr.join (', '));
Arr.splice (2, 1);
Console.log (' array after deletion of elements ');
Console.log (Arr.join (', '));
</script>

Results

Arr.splice (2,1): Delete the element starting with index=2, 1: The number of deleted elements is 1. If you want to delete the elements after 2 can be written like this: Arr.splice (2,2);

    <script>        var arr = [1, 2, ' A ', ' B '];        Console.log (' original array ');        Console.log (Arr.join (', '));        Arr.splice (2, 1, ' Wolfy ');        Console.log (' array after deletion of elements ');        Console.log (Arr.join (', '));     </script>

In this example, we delete the element with index 2 and add the new element ' Wolfy '. Somewhat similar to replacement.

Summarize

Removes array-specific elements from the project, or is more commonly used, and is good at using some native methods of array.

[Js] To delete an array of specified elements

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.