Using JS to delete array elements

Source: Internet
Author: User

Delete an item of an array

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

//删除起始下标为1,长度为1的一个值(len设置1,如果为0,则数组不变)vararr = [‘a‘,‘b‘,‘c‘,‘d‘];arr.splice(1,1);console.log(arr);  //[‘a‘,‘c‘,‘d‘]; //删除起始下标为1,长度为2的一个值(len设置2)vararr2 = [‘a‘,‘b‘,‘c‘,‘d‘]arr2.splice(1,2);console.log(arr2); //[‘a‘,‘d‘]

  

Replace

//替换起始下标为1,长度为1的一个值为‘ttt’,len设置的1vararr = [‘a‘,‘b‘,‘c‘,‘d‘];arr.splice(1,1,‘ttt‘);console.log(arr);        //[‘a‘,‘ttt‘,‘c‘,‘d‘] vararr2 = [‘a‘,‘b‘,‘c‘,‘d‘];arr2.splice(1,2,‘ttt‘);console.log(arr2);       //[‘a‘,‘ttt‘,‘d‘] 替换起始下标为1,长度为2的两个值为‘ttt’,len设置的1

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

vararr = [‘a‘,‘b‘,‘c‘,‘d‘];arr.splice(1,0,‘ttt‘);console.log(arr);        //[‘a‘,‘ttt‘,‘b‘,‘c‘,‘d‘] 表示在下标为1处添加一项‘ttt‘

Using JS to delete array 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.