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'
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'
The following code deletes the specified values in the array.
The Code is as follows:
Array. Prototype. Remove = function (B ){
VaR A = This. indexof (B );
If (A> = 0 ){
This. splice (A, 1 );
Return true;
}
Return false;
};
In the future, you can use this function to delete the values in the array.