JS a few ways to delete an item or items in an array

Source: Internet
Author: User

The Splice method in 1:js

Splice (Index,len,[item]) Comment: The method changes the original array.

The splice has 3 parameters, which can also be used to replace/delete/Add one or more values within an array

Index: Array start subscript len: replace/Delete length item: Replace value, delete action if item is empty

such as: arr = [' A ', ' B ', ' C ', ' d ']

Delete----item is not set

Arr.splice (//[' A ', ' C ', ' d ') deletes a value starting from subscript 1, length 1, Len set 1, and if 0, the array is unchanged

Arr.splice (//[' A ', ' d ') removes a value from the starting subscript of 1, length 2, and Len sets 2

Replace----item for the replacement value

Arr.splice ("TTT")//[' a ', ' ttt ', ' C ', ' d '] replace the starting subscript with 1, a value of length 1 for ' TTT ', Len set 1

Arr.splice ("TTT")//[' a ', ' ttt ', ' d '] replace the starting subscript with 1, length 2 of two values for ' TTT ', Len set 1

Add----len set to 0,item for added value

Arr.splice (1,0, ' TTT ')//[' a ', ' ttt ', ' B ', ' C ', ' d '] means adding a ' ttt ' at subscript 1

It looks like splice is the most convenient.

2:delete Delete Removes the element from the array, it will set the value of the subscript to undefined, and the length of the array will not change

such as: delete arr[1]//[' A ',, ' C ', ' d '] in the middle of the two comma, the length of the array is unchanged, there is a undefined

JS a few ways to delete an item or items in an array

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.