Home > Web Front-end > JS Tutorial > Summary of several methods of deleting arrays in js_javascript skills

Summary of several methods of deleting arrays in js_javascript skills

WBOY
Release: 2016-05-16 16:58:50
Original
1148 people have browsed it

var arr=['a','b','c'];
To delete the 'b', there are two methods:

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 original array remains unchanged. At this time, you can use

Copy code The code is as follows:
for(index in arr)
{
document.write('arr [' index ']=' arr[index]);
}

This traversal method skips undefined elements
* This method will be supported by IE4.o and later

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 (counting from 0), which is the second element of the array

The second 1 is the number of deleted elements. Only one element is deleted here, namely 'b';

At this time, you can traverse the array elements in the normal way of traversing the array, such as for, because the deleted elements are not retained in the array

* This method is only supported after IE5.5

It is worth mentioning that while the splice method deletes array elements, it can also add new array elements

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

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

JavaScript truncates an array by setting the length property of the array. The only way to shorten the length of an array is to use the delete operator to delete an element in the array. Although that element becomes undefined, the length property of the array does not There are two ways to delete elements and change the array length without changing.


Copy code The code is as follows:
/*
*Method: Array.remove(dx)
* Function: delete array element.
* Parameter: dx subscript of deleted element.
* Return: modify the array on the original array
*/
//Often used is to reconstruct the array through traversal.

Array.prototype.remove=function(dx)

 {
 if(isNaN(dx)||dx>this.length){return false;}
 for(var i=0,n =0;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 index 0
alert("elements: " a "nLength: " a.length ; > *Method: Array.baoremove(dx)
* Function: delete array element.
* Parameter: dx subscript of deleted element.

* Return: modify the array on the original array.
*/
  //We can also use splice to implement. 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);


We know that in IE5 or lower versions, JavaScript's Array (array) object does not provide a ready-made method 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 actually removing an item of the array from the array members). Maybe you would think of using a loop to reassign the array. This is certainly possible, but it is very inefficient.

Below we introduce how to use the two methods slice and concat of the Array object to customize the deletion of the array.

The specific code is as follows, please pay attention to the comments inside.




Copy code

The code is as follows:

Array.prototype.del=function(n) { //n represents the number, starting from 0.
//Prototype is the object prototype. Note how to add custom methods to the object here.
if(n<0) //If n<0, no operation is performed.
Return this;
else
return this.slice(0,n).concat(this.slice(n 1,this.length)); A new array composed of two or more arrays.
     Here is a new array composed of returning this.slice(0,n)/this.slice(n 1,this.length)
                               
Slice method: Returns a segment of an array, with two parameters specifying the start and end positions respectively.
 */
}
//Let’s try this method of adding it yourself
var test=new Array(0,1,2,3,4,5);
test =test.del(3); //Counting from 0, the 4th item is deleted here.
alert(test);


In this way, we only use the two methods of the Array object to achieve our requirements.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template