Deleting array elements in Javascript-delete vs splice

Source: Internet
Author: User

The JavaScript array removes elements with Array.splice (start, deletecount);

-----------------------------------------------------

deleteWould delete the object property, but would not be reindex the array or update its length. This makes it appears as if it is undefined:

>MyArray= [A, ' B ',  ' C ' ,  ' d '   [ "a" ,  "B" ,  "C" ,  "D" ]>delete Myarray[0]< Span class= "PLN" > true> Myarray[0] undefined     

Note that it's not on fact set to the value undefined , rather the removed from the array, making it appear undefined. The Chrome dev tools make this distinction clear by printing when empty logging the array.

> myArray[0] undefined> myArray [empty, "b", "c", "d"]

myArray.splice(start, deleteCount)Actually removes the element, reindexes the array, and changes its length.

>MyArray= [A, ' B ', C, ' d '] [A,  "B" ,  "C" ,  "D" ]> Myarray. (0, 2   [ "a" , "B" ]> MyArray [ "C" ,  "D"  ]   

Deleting array elements in Javascript-delete vs splice

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.