JScript built-in object Array element deletion method _ javascript skills

Source: Internet
Author: User
JScript built-in object Array element deletion method we know that JScript provides us with a built-in Array object Array. In addition to constructor, length, and prototype, the Array object also provides 13 methods by default: concat, join, pop, push, reverse, shift, slice, sort, splice, toLocaleString, toString, unshift, and valueOf, but the delete method is not provided.

If you are familiar with JavaScript, we will immediately say that the system provides a delete operation that can be used to delete elements in the array. Yes, there is indeed a delete in the JS system that can delete elements in the array. However, this deletion is difficult to use. it can indeed delete elements, but it does not update the element counter of the Array object. For example, we execute:

Var ary = ['A', 'B', 'C'];
Delete ary [1];
If the deletion is performed correctly, we want to get a new array with two elements ['A', 'C'] with a length of 2. After the execution, we get the array of two elements ['A', 'C'], but the length of the new array is still 3 !. At the same time, we execute ary. toString () will get "a, c", which also indicates that the Array counter is 3, because the Array toString () actually executes Array. join (',').

Such deletion will be very depressing when we use for (;) to traverse the array, and we may easily be killed by an undefined value. So how can we get the size of the synchronized array after deleting the array elements? Since the pop and shift functions provided by Array can "really" delete the elements of the Array, we can use them to expand a remove function.

However, pop and shift can only delete elements from both ends of the array. Therefore, we need to sort out the array before deleting it. the code for implementing the remove method is as follows:

Array. prototype. remove = function (obj)
{
For (var I = 0; I <this. length; ++ I)
{
If (this [I] = obj)
{
If (I> this. length/2)
{
For (var j = I; j <this. length-1; ++ j)
{
This [j] = this [j + 1];
}
This. pop ();
}
Else
{
For (var j = I; j> 0; -- j)
{
This [j] = this [j-1];
}
This. shift ();
}
Break;
}
}
};
The purpose of moving the array is to delete the elements without changing the relative positions of the remaining elements. Otherwise, you only need to delete the elements swap to pop or shift at both ends.

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.