JS Delete Array element method

Source: Internet
Author: User
Tags array length

var arr=[' http://down.111cn.net ', ' 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]);

Method Two

<script language= "Web Effects" type= "Text/javascript" >
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 return to This.slice (0,n)/this.slice (n+1,this.length)
Consists of a new array, 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.
*/
}

Let's try this one's own way.

var test=new array (0,1,2,3,4,5);
Test=test.del (3); From 0, this is the deletion of item 4th.
alert (test);
</script>


Method Three

Arrayobj.splice (Start, DeleteCount, [item1[, item2[, ...) [, Itemn]]]

<script>
var s=new array (5);
s[0]= ' www.111cn.net ';
s[1]= ' 1 ';
s[2]= ' 2 ';
s[3]= ' www.111cn.net ';
s[4]= ' 4 ';
S.splice (1,1)//removal s[1]
alert (s.length);//value is 4
Alert (s[1]);//The value is 2
</script>


About Splice Function parameter description

Where Arrayobj must be selected. An array object.
Start is a required option. Specifies that the starting position of the element is removed from the array, with a minimum value of 0.
DeleteCount is a required option. The number of elements to remove.
Item1, item2,.. ., itemn is optional. The new element to insert at the location of the removed element.
The splice function method in JavaScript can modify arrayobj by removing the specified number of elements from the start position and inserting new elements. The return value is a new array object that consists of the removed element.

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.