Delete methods for elements in the JScript built-in object array _javascript tips

Source: Internet
Author: User
We know that JScript provides us with a built-in array object. In addition to providing 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 did not provide a delete method.

If you are familiar with JavaScript, you will immediately say that the system provides a delete operation that can be used to remove elements from an array. Yes, the JS system does have a delete that deletes the elements in the array. But this deletion is very difficult to use, it can indeed delete elements, but it does not update the array object's element counters. For example we perform:

var ary = [' A ', ' B ', ' C '];
Delete Ary[1];
If the correct execution of the deletion, we would like to get a new array, he has two elements [' A ', ' C '], the length of 2. But after the execution we did get two elements [' A ', ' C '] array, but the length of the new array ary.length incredibly still 3!. At the same time we execute ary.tostring () will get "A,,c", which also indicates that the array counter is still 3, because the array's toString () actually executes the array.join (', ').

Such deletes are used in our for (;;) When traversing an array, it will be very depressing and we may easily be killed by a undefined value. So how do you get the size of the synchronized array after you delete the array elements? Because the array itself provides the pop and shift two functions that can "really" delete the elements of arrays, we can use them to augment a remove function ourselves.

But pop and shift can only delete elements from both ends of the array, so we need to do some sorting before we delete the code for the Remove method 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 simply to delete the elements and not change the relative positions of the remaining elements, or simply swap the elements you want to remove to the ends of pop or shift below.

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.