Array
JavaScript truncating an array by setting the length property of an array is the only one by one way to shorten the length of an array. If you use the delete operator to delete elements in an array, the length property of the array does not change the two deletion elements, although that element becomes undefined. The array length also changes the method.
<script>
/*
* Method: Array.remove (DX)
* Function: Deletes an array element.
* Parameter: DX Deletes the subscript of the element.
* Return: Modify the array on the original array
*/
It is commonly used to refactor arrays by traversing them.
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 with subscript 0
Alert ("Elements:" +a+ "\nlength:" +a.length);
/*
* Method: Array.baoremove (DX)
* Function: Deletes an array element.
* Parameter: DX Deletes the subscript of the element.
* Return: Modifies the 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); Delete the element with subscript 1
Alert ("Elements:" +b+ "\nlength:" +b.length);
</script>