A powerful function of an array splice,[, delete, change]

Source: Internet
Author: User

 1 //  var a = [n/a]; 
2 // a.splice (0); 3 // Console.log (a); >>[]
4 // A.splice (1); 5 // Console.log (a); >>[1]
6 // A.splice (0,1); 7 // Console.log (a); >>[2,3]
The first parameter represents starting from the beginning, the second parameter means deleting a few, if not written, or just writing a parameter 0, will delete all.
1//var a = [[+]
2// A.splice (0,0,6); 3 // Console.log (a); >>[6, 1, 2, 3]

The third parameter indicates that a value is added, which is represented in this line of code, starting with the No. 0, deleting 0 values and adding a 6 from the No. 0 index.

1 // var a = [[+] 2 // A.splice (0,1,6); 3 // Console.log (a); >>[6, 2, 3] 4 // A.splice (1,1,6); 5 // Console.log (a); >>[1, 6, 3]

If the second argument is not written in 0, then the third argument can be used as a replacement, which is, of course, a small trick.

1 // A.splice (1,0,1.1,1.2,1.3); 2 // Console.log (a); >>[1, 1.1, 1.2, 1.3, 2, 3]
You can add an arbitrary value after the second argument



A powerful function of an array splice,[, delete, change]

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.