1. Clear the array
var ary = [1,2,3,4]; ary.splice(0,ary.length);//Clear the array console.log(ary); // Output [], empty the array, that is, it is cleared
2. Delete array elements
var ary = [1,2,3,4]; ary.splice(0,1); or ary.splice($.inArray(2, ary), 1); where $.inArray(2, ary) is used to find the index position of an element in the array.
Three, several ways to delete arrays in js
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 normal way, such as for, because the deleted elements are
Not preserved 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'
In addition, JavaScript truncates arrays by setting the length property of the array, which is the only way to shorten the length of the 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 the two methods to delete elements, and the length of the array also changes.
/* * Method: Array.remove(dx) * Function: Delete array elements. * Parameters: dx delete subscripts of elements. * Return: Modify arrays on the original array*/// Often use 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); //Delete the element with subscript 0 alert("elements: "+a+"nLength: "+a.length);Example 2,
/* * Method: Array.baoremove(dx) * Function: Delete array elements. * Parameters: dx delete subscripts of elements. * Return: Modify array on the original array. */ // You can also use splice to implement. Array.prototype.baoremove = function(dx) { // www.VeVB.COMif(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);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 and the length of the array has not changed.
In fact, you can add a delete method to the array yourself (note that this refers to removing an item of the array from the array member). Perhaps, you will think of using loops to reassign values to an array. This is certainly OK, but it is very inefficient.
The following introduces the method of customizing the deletion of arrays using two methods of Array object slice and concat.
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; elsereturn this.slice(0,n).concat(this.slice(n+1,this.length));/* concat method: Returns a new array, which is composed of two or more 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. */}//The method of adding it yourself var test=new Array(0,1,2,3,4,5);test=test.del(3); //From 0, delete the 4th item. alert(test);The above code only flexibly uses two methods of Array object, which realizes the basic requirements, which is good.
The above simple method of deleting array elements and clearing arrays (must read) in JS is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.