Deletion of elements in the array of the built-in object in JScript

Source: Internet
Author: User

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 ary. length.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, so we need to sort out the array before deleting to implement the Remove MethodCodeAs 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 ();
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: 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.