var arr=['a','b','c'];
To delete 'b', there are two ways:
1.delete method: delete arr[1]
In this way, the length of the array remains unchanged. At this time, arr[1] becomes undefined, but it also has the advantage that the index of the array remains unchanged. At this time, you have to traverse the array elements to use
for(index in arr){ document.write('arr['+index+']='+arr[index]);}This traversal method skips undefined elements in it
* This method will be supported in the future.
2. Array object splice method: arr.splice(1,1);
In this way, the length of the array is changed accordingly, but the original array index also changes accordingly
The first 1 in the splice parameter is the starting index of the delete (calculated from 0), which is the second element of the array.
The second 1 is the number of elements that are deleted, and only one element is deleted here, that is, 'b';
At this time, it is possible to traverse the array elements in a normal way, such as for, because the deleted elements are not retained in the array.
* This method will only be supported after IE5.5
It is worth mentioning that while deleting array elements, splice method can also add array elements.
For example, arr.splice(1,1,'d','e'), d, and e are added to the array arr
The result array becomes arr:'a','d','e','c'
JavaScript truncating an array by setting the length attribute of an array is the only way to shorten the length of an array. If you use the delete operator to delete an element in the array, although that element becomes undefined, the length attribute of the array does not change two times. A method to delete elements and change the length of the array.
/* * Method: Array.remove(dx) * Function: Delete array elements. * Parameters: dx delete subscripts of elements. * Return: Modify arrays on the original array*/ // Often used is to reconstruct arrays 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 with subscript 0 alert("elements: "+a+" nLength: "+a.length); /* * Method: Array.baoremove(dx) * Function: Delete array elements. * Parameters: dx delete subscripts of elements. * Return: Modify array on the original array. */ // We can also use splice to implement it. 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 of 1 alert(" elements: "+b+"nLength: "+b.length);We know that in IE5 or lower, JavaScript's Array object does not provide an off-the-shelf way to delete array elements. In the IE5.5+ version, although there is a splice method, it does not delete a certain item (or several items), but only clears the value of a certain item (or several items), which means that the item still exists , the length of the array has not changed.
In fact, we can add a delete method to the array ourselves (note that this refers to removing an item of the array from the array member). Maybe you will think of using loops to reassign arrays, which is certainly OK, but it is inefficient.
Below we introduce two methods of using slice and concat to customize the deletion of arrays.
The specific code is as follows, please pay attention to the comments inside.
Array.prototype.del=function(n) { //n represents which term, starting from 0. //prototype is the object prototype, pay attention to the method of adding custom methods to the object here. if(n<0) //If n<0, nothing is done. return this; else return this.slice(0,n).concat(this.slice(n+1,this.length)); /* concat method: Return a new array, which is composed of two or more Combined arrays. Here is a new array composed of this.slice(0,n)/this.slice(n+1,this.length). In the middle, the nth item is missing. slice method: Returns a segment of an array with two parameters, specifying the starting and ending positions respectively. */}// Let's try this method of increasing it yourself var test=new Array(0,1,2,3,4,5); test=test.del(3); //From 0, This is the deletion of item 4. alert(test);In this way, we only flexibly use the two methods of the Array object to achieve our requirements.