The split usage in JS and the deletion of elements in the array

Source: Internet
Author: User

JS in Split usage

  • <script language="JavaScript" >
  • function Spli () {
  • datastr="2,2,3,5,6,6";
  • var str= new Array ();
  • Str=datastr.split (",");
  • For (i=0;i<str.length; i++)
  • {
  • document.write (str[i]+"<br/>");
  • }
  • }
  • Spli ();
  • </script>

JS Delete array elements

var arr=[' A ', ' B ', ' C '];
To remove the ' B ', there are two ways:

1.delete method: Delete Arr[1]
This way the array length is not changed, at this point arr[1] becomes undefined, but also has the benefit of the original array index also remains unchanged, at this time to repeat the group elements can be used
For (index in ARR)
document.write (' arr[' +index+ ']= ' +arr[index]);
This traversal way skips the elements of undefined

* This method IE4.O support after all.

2. Array Object Splice method: Arr.splice (n);
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 the deletion (from 0), where the second element of the array
The second 1, is to delete the number of elements, here only one element, that is, ' B ';
Traversing an array element can be used to iterate through an array, such as for, because the deleted element
The array does not retain

* This method is only supported after IE5.5

It is worth mentioning that the splice method can also add elements to an array while deleting an array element
For example, Arr.splice (' d ', ' e '), d,e two elements are added to the array arr.
The resulting array becomes arr: ' A ', ' d ', ' e ', ' C '

The split usage in JS and the deletion of elements in the array

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.