Delete element from JS Array

Source: Internet
Author: User

VaR arr = ['A', 'B', 'C']; to delete 'B', there are two methods: 1. delete method: delete arr [1]. In this way, the length of the array remains unchanged. At this time, arr [1] is changed to undefined, but it also benefits that the index of the original array remains unchanged, in this case, you can use for (index in ARR) document to traverse the array element. write ('Arr ['+ index +'] = '+ arr [Index]); this Traversal method skips undefined elements * ie4.o later supports 2. array object splice method: arr. splice (); in this way, the array Length changes accordingly, but the original array index also changes the first 1 in the splice parameter, which is the initial index to be deleted (from 0 ), here is the second 1 of the second element of the array, which is the number of elements to be deleted. Here, only one element is deleted, that is, 'B'. In this case, the array elements can be traversed in a normal way, for example, because the deleted element is not retained in the array * This method ie5.5 is supported. It is worth mentioning that the splice method deletes the array element while, you can also add array elements such as arr. splice (, 'D', 'E'), D, and E are added to the array arr. The result array is changed to ARR: 'A', 'D ', 'E', 'C'Additional article: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 array elements. * parameter: DX deletes the subscript of an element. * return: Modify the array on the original array * // It is often used to traverse and reconstruct the array. array. prototype. remove = function (dx) {If (isnan (dx) | DX> This. length) {return false;} For (VAR I = 0, n = 0; I

Another article:

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.

--------------------------------------------------------------
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 );
Test = test. Del (3); // The value starts from 0. In this case, 4th items are deleted.
Alert (test );

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.