JS simple way to remove array elements and empty arrays (must) _javascript tips

Source: Internet
Author: User
Tags array length javascript array

One, empty the array

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

Second, 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 an array.

Three, JS delete the array several methods

var arr=[' A ', ' B ', ' C '];
There are two ways to delete a ' B ' in it:

1,delete method: Delete Arr[1]

This way the array length does not change, at this time arr[1] becomes undefined, but also has the advantage the index of the original array also remains unchanged, at this time to enumerate the elements of the group can be used

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

This traversal way skips over the elements of the undefined
* This way IE4.O after the support of the

2, Array Object Splice method: Arr.splice (1,1);

This way the array length changes, but the original array index changes accordingly

The first 1 in the splice parameter is the starting index of the deletion (starting from 0), which is the second element of the array

The second 1 is to delete the number of elements, where only one element is deleted, i.e. ' B ';

Traversing an array element at this time can be used to traverse the array in a normal way, for example, because the deleted element

The array does not hold

* This method IE5.5 later support

It is worth mentioning that the splice method can be added to an array element while deleting an array element

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

The resulting array becomes arr: ' A ', ' d ', ' e ', ' C '

In addition, JavaScript truncates the array by setting the length property of the array, which is the only one by one way to shorten the length of the array.
If you use the delete operator to delete the elements in an array, although that element becomes undefined, the length property of the array does not change the two deletion elements, and the array lengths change.

/
* Method: Array.remove (DX)
* Feature: Deletes an array element.
* Parameter: DX Deletes the subscript of the element.
* Return: Modify the array on the original
array
////often by traversing, refactoring the 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 the element alert with subscript 0
("Elements:" +a+ "nlength:" +a.length);
 

Example 2,

/
* Method: Array.baoremove (DX)
* Feature: Deletes an array element.
* Parameter: DX Deletes the subscript of the element.
* Return: Modifies the array on the original array.
* *///
can also be implemented with splice.
Array.prototype.baoremove = function (dx)
{//Www.jb51.net
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 alert with subscript 1
("Elements:" +b+ "nlength:" +b.length);
 

In IE5 or lower versions, the JavaScript array object does not provide a ready-made way to delete an array element. In the ie5.5+ version, although there is a splice method, it is not deleting an item (or several items), but simply clearing the value of an item (or items), meaning that the item still exists, and the length of the array does not change.

In fact, you can add a deletion method to your own array (note that this refers to the true removal of an item from an array member). It might be possible to think of a loop to reassign the array, but it's inefficient.

The following describes a method that uses the array object's two methods slice, concat to customize the deletion of an array.

Array.prototype.del=function (n) {//n represents the first few items, starting from 0.
//prototype is an object prototype, note the method for adding a custom method to an object.
if (n<0)//If n<0, no action is made. return this
;
else return
This.slice (0,n). Concat (This.slice (n+1,this.length));
/*
Concat Method: Returns a new array, which is a combination of two or more arrays. This
is the new array that returns the This.slice (0,n)/this.slice (n+1,this.length)
 , which, in the middle, is just missing the nth item.
Slice method: Returns a section of an array, two parameters, specifying the start and end positions respectively.
*/
}
Own added Method
var test=new Array (0,1,2,3,4,5);
Test=test.del (3); From 0, delete item 4th.
alert (test);
 

The above code, only flexible use of the array object two methods, then realize the basic requirements, good.

The above JS Delete array elements, empty the array of simple method (must see) is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.