js A simple way to delete array elements and empty arrays of must see

  • 2021-07-06 09:41:22
  • OfStack

1. Empty the array


var ary = [1,2,3,4]; 
ary.splice(0,ary.length);// Empty an array  
console.log(ary); //  Output  [] Empty array, that is, it is emptied 

2. Delete array elements


var ary = [1,2,3,4]; 
ary.splice(0,1);
 Or  ary.splice($.inArray(2, ary), 1);  Among them $.inArray(2, ary) Used to find the index position of an element in an array. 

3. Several methods of deleting arrays in js

var arr=['a','b','c'];
There are two ways to delete 'b':

1. delete method: delete arr [1]

In this way, the length of the array is unchanged, and arr [1] becomes undefined at this time, but it is also beneficial that the index of the original array remains unchanged, and it is necessary to traverse the array elements at this time


for(index in arr)
{
 document.write('arr['+index+']='+arr[index]);
}

This traversal skips the elements of undefined
* This mode is supported after IE4.o

2. Array object splice method: arr. splice (1, 1);

In 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 deletion (from 0), in this case the second element of the array

The second 1 is the number of deleted elements, and only one element is deleted here, that is, 'b';

At this point, you can traverse the array elements in a normal way, such as for, because the deleted elements are in the

Is not retained in the array

* This method is supported after IE 5.5

It is worth 1 to mention that the splice method can add array elements while deleting them

For example, arr. splice (1, 1, 'd', 'e'), d and e are added to the array arr

The resulting array becomes arr: 'a', 'd', 'e', 'c'

In addition, JavaScript truncates an array by setting its length property, which is the only 11 ways to shorten the length of an array.
If you use the delete operator to delete an element in an array, the length attribute of the array does not change the two methods of deleting the element and changing the length of the array, although that element becomes undefined.


/*
    * Method :Array.remove(dx)
    * Function : Delete Array Elements .
    * Parameter :dx Delete the subscript of an element .
    * Return : Modify the array on the original array 
*/
// It is often used by traversing , Reconstruct array .
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 subscript as 0 Elements of 
   alert("elements: "+a+"nLength: "+a.length);
 

Example 2,


/*
    * Method :Array.baoremove(dx)
    * Function : Delete Array Elements .
    * Parameter :dx Delete the subscript of an element .
    * Return : Modify the array on the original array .
    */
   // You can also use splice To realize .
   Array.prototype.baoremove = function(dx)
   { // www.ofstack.com
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 subscript as 1 Elements of 
   alert("elements: "+b+"nLength: "+b.length);
 

In IE 5 or earlier, the Array (Array) object of JavaScript does not provide a ready-made method for deleting array elements. In the version of IE 5.5 +, although there is an splice method, it does not delete one item (or items), but only clears the value of one item (or items), that is, the item still exists and the length of the array does not change.

In fact, you can add a deletion method to the array yourself (note that this means actually removing an item from the array members). You might think of using loops to re-assign values to arrays, which is fine, but inefficient.

The following describes how to customize the method of deleting arrays using two methods of the Array object, slice and concat.


Array.prototype.del=function(n) {   //n Indicates which item, starting from 0 Start counting. 
//prototype For object prototypes, notice the method here for adding custom methods to objects. 
   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: Returns 1 A new array, which is composed of two or more groups. 
 This is the return this.slice(0,n)/this.slice(n+1,this.length)
  Composed of a new array, in the middle, just missing the first n Item. 
   slice Methods:   Return 1 Of an array of 1 Segment, two parameters that specify the start and end positions, respectively. 
*/
}
// Self-increasing method 
var test=new Array(0,1,2,3,4,5);
test=test.del(3);   // From 0 From the count, delete the 4 Item. 
alert(test);
 

Above code, only flexible use of Array object two methods, it realizes the basic requirements, good.


Related articles: