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

May 16, 2016 pm 07:17 PM

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.

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

10 jQuery Syntax Highlighters 10 jQuery Syntax Highlighters Mar 02, 2025 am 12:32 AM

10 jQuery Syntax Highlighters

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

10  JavaScript & jQuery MVC Tutorials 10 JavaScript & jQuery MVC Tutorials Mar 02, 2025 am 01:16 AM

10 JavaScript & jQuery MVC Tutorials

See all articles