Introduction to JS functions for deleting array elements

Source: Internet
Author: User

Split converts the string into an array and outputs the Code:

Copy codeThe Code is as follows:
<Script language = "javascript">
Function spli (){
Datastr = "2, 2, 3, 5, 6 ";
Var str = new Array ();
Str = datastr. split (",");
For (I = 0; I <str. length; I ++)
{
Document. write (str [I] + "<br/> ");
}
}
Spli ();
</Script>

Js deletes array elements:

Var arr = ['A', 'B', 'C'];
To delete 'B', you can use either of the following methods:

1. delete method: delete arr [1]

In this way, the length of the array remains unchanged. In this case, the arr [1] is changed to undefined, but the index of the original array remains unchanged. In this case, you need to traverse the array element before using it.

Copy codeThe Code is as follows:
For (index in arr)
{
Document. write ('Arr ['+ index +'] = '+ arr [index]);
}


This Traversal method skips the undefined element.

* IE4.o is supported later.

2. array object splice method: arr. splice );

In this way, the length of the array is changed, but the original array index is also changed.
The first 1 in the splice parameter is the initial index to be deleted (from 0), and the second element of the array.
The second one is the number of elements deleted. Only one element is deleted here, that is, 'B ';
In this case, the elements in the array can be traversed in the normal way, for example, because the deleted elements are
The array is not retained.

* This method is supported only after IE5.5.

It is worth mentioning that the splice method can also add array elements while deleting array elements.
For example, arr. splice (, 'D', 'E'), d, and e are added to the array arr.
The result array is changed to arr: 'A', 'D', 'E', 'c'

JavaScript truncates an array by setting the length attribute of the array. It is the only method to shorten the length of the array. if you use the delete operator to delete elements in an array, although the element becomes undefined, The length attribute of the array does not change the two ways to delete elements, and the length of the array also changes.

/*
* Method: Array. remove (dx)
* Function: delete an array element.
* Parameter: dx deletes the subscript of an element.
* Return value: Modify the array on the original array.
*/
  
// It is often used to reconstruct the array through traversal.

Copy codeThe Code is as follows:
Array. prototype. remove = function (dx)
{
If (isNaN (dx) | dx> this. length) {return false ;}
For (var I = 0, n = 0; I <this. length; I ++)
{
If (this [I]! = This [dx])
{
This [n ++] = this [I]
}
}
This. length-= 1
}
A = ['1', '2', '3', '4', '5'];
Alert ("elements:" + a + "nLength:" + a. length );
A. remove (0); // Delete the element whose subscript is 0
Alert ("elements:" + a + "nLength:" + a. length );

Copy codeThe Code is as follows:
/*
* Method: Array. baoremove (dx)
* Function: delete an array element.
* Parameter: dx deletes the subscript of an element.
* Return value: Modify the array on the original array.
*/
  
// We can also use splice.
  
Array. prototype. baoremove = function (dx)
{
If (isNaN (dx) | dx> this. length) {return false ;}
This. splice (dx, 1 );
}
B = ['1', '2', '3', '4', '5'];
Alert ("elements:" + B + "nLength:" + B. length );
B. baoremove (1); // Delete the element whose subscript is 1.
Alert ("elements:" + B + "nLength:" + B. length );

We know that in IE5 or earlier versions, the Array (Array) object of JavaScript does not provide a ready-made method to delete Array elements. In IE5.5 + versions, although there is a splice method, it does not delete one or more items, but simply clears the value of one or more items, that is to say, this item still exists, and the length of the array has not changed.

In fact, we can add a deletion method for the array by ourselves (note that this refers to removing a certain item of the array from the array member ). You may think of a loop to re-assign values to the array. This is certainly possible, but the efficiency is very low.

The following describes how to use two methods of the Array object: slice and concat to customize the method for deleting arrays.

The specific code is as follows. Note the annotations.

Copy codeThe Code is as follows:
Array. prototype. del = function (n) {// n indicates the number of items, starting from 0.
// Prototype is the object prototype. Note that the custom method is added for the object.
If (n <0) // if n <0, no operation is performed.
Return this;
Else
Return this. slice (0, n). concat (this. slice (n + 1, this. length ));
/*
Concat method: returns a new array consisting of two or more arrays.
Here we return this. slice (0, n)/this. slice (n + 1, this. length)
In the middle of the new array, the nth entry is missing.
Slice Method: return a segment of an array with two parameters, respectively specifying the start and end positions.
*/
}
// Let's try this method we have added.
Var test = new Array (0, 1, 2, 3, 4, 5 );
Test = test. del (3); // The value starts from 0. In this case, 4th items are deleted.
Alert (test );

In this way, we only use the two methods of the Array object to implement our requirements.

Related Article

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.