JS deletes array elements.

Source: Internet
Author: User

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.
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'

 

Bytes ----------------------------------------------------------------------------------------------

Javascript deletes array elements (with VBScript)
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.

<SCRIPT>
/*
* 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.
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 );

/*
* 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 );
</SCRIPT>

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.