Home > Web Front-end > JS Tutorial > Deletion method of elements in JScript built-in object Array_javascript skills

Deletion method of elements in JScript built-in object Array_javascript skills

WBOY
Release: 2016-05-16 19:17:51
Original
1118 people have browsed it

We know that JScript provides us with a built-in array object Array. 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 does not provide delete method.

If you are familiar with JavaScript, you will immediately say that the system provides a delete operation that can be used to delete elements in an array. Yes, there is indeed a delete in the JS system that can delete elements in the array. But 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 executed correctly, we hope to get A new array has two elements ['a', 'c'] and has a length of 2. But after execution, we did get an array with two elements ['a', 'c'], but the length of this new array is still 3!. At the same time, when we execute ary.toString(), we will get "a,,c", which also shows that the counter of the array is still 3, because Array's toString() actually executes Array.join(',').

Such deletion will be very frustrating 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 itself can "really" delete elements of the array, we can use them to expand 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 of the array before deleting. The code to implement the remove method is as follows:

Array.prototype. remove = function(obj)
{
for (var i=0; i {
if (this[i] == obj)
{
                                                                                                     {
this[j] = this[ j 1]; }
This.pop (); }
Else
{
for (var j = i; j> 0; --j)
{
                                                                                       break;
}
}
} ;
The purpose of moving and sorting the array is just to not change the relative positions of the remaining elements after deleting the elements. Otherwise, just swap the elements that need to be deleted to the pop or shift below the two ends.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template