var arr=[' A ', ' B ', ' C '];
To remove the ' B ', there are two ways:
1.delete method: Delete Arr[1]
This way the array length is not changed, at this point arr[1] becomes undefined, but also has the benefit of the original array index also remains unchanged, at this time to repeat the group elements can be used
For (index in ARR)
{
document.write (' arr[' +index+ ']= ' +arr[index]);
}
This traversal way skips the elements of undefined
* This method IE4.O support after all.
2. Array Object Splice method: Arr.splice (n);
This way the array length changes accordingly, but the original array index also changes accordingly
The first 1 in the splice parameter is the starting index of the deletion (from 0), where the second element of the array
The second 1, is to delete the number of elements, here only one element, that is, ' B ';
Traversing an array element can be used to iterate through an array, such as for, because the deleted element
The array does not retain
* This method is only supported after IE5.5
It is worth mentioning that the splice method can also add elements to an array while deleting an array element
For example, Arr.splice (' d ', ' e '), d,e two elements are added to the array arr.
The resulting array becomes arr: ' A ', ' d ', ' e ', ' C '
<big> a:</big>
JavaScript truncates an array by setting the length property of the array is only one by one ways to shorten the length of the array. If you use the delete operator to delete an element in an array, although that element becomes undefined, the length property of the array does not change the two delete elements. The array length also changes the method.
/*
* Method:array.remove (DX)
* Function: Delete array element.
* Parameter: The subscript of the DX delete element.
* Return: Modify array on the original array
*/
It is commonly used to reconstruct an 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); Remove the element labeled 0
Alert ("Elements:" +a+ "nlength:" +a.length);
/*
* Method: Array.baoremove (DX)
* Function: Delete array element.
* Parameter: The subscript of the DX delete element.
* Return: Modifies an array on the original array.
*/
We can also use splice to achieve.
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); Remove the element labeled 1
Alert ("Elements:" +b+ "nlength:" +b.length);
We know that in IE5 or lower versions, the array (array) object of JavaScript does not provide a ready-made way to delete array elements. In ie5.5+ 's version, although there is a splice method, it does not delete an item (or items), but simply clears the value of an item (or items), that is, the item still exists, and the length of the array does not change.
In fact, we can add a Delete method for our own array (note that this refers to the actual removal of an item of an array from the array member). You might think of using loops to re-assign values to an array, which is fine, but inefficient.
Here we introduce the method of slice, concat to delete an array using the two methods of the array object.
The specific code is as follows, please note the comment inside.
--------------------------------------------------------------
Array.prototype.del=function (n) {//n represents the first item, starting from 0.
Prototype is the object prototype, note the method of adding a custom method to the object.
if (n<0)//If n<0, no action is made.
return this;
Else
Return This.slice (0,n). Concat (This.slice (n+1,this.length));
/*
Concat method: Returns a new array, which is a combination of two or more arrays.
This is the return This.slice (0,n)/this.slice (n+1,this.length)
A new array of elements, which, in the middle, is just less than the nth item.
Slice method: Returns a section of an array of two parameters that specify the starting and ending positions, respectively.
*/
}
Let's try this self-increasing method.
var test=new Array (0,1,2,3,4,5);
Test=test.del (3); From 0 onwards, this is the deletion of the 4th item.
alert (test);
------------------------------------------------------------------------------
In this way, only the flexible use of the array object two methods, the implementation of our requirements.
Http://www.cnblogs.com/qiantuwuliang/archive/2010/09/01/1814706.html
JS Delete array several methods