JS How to remove an item from a specified index in an array

Source: Internet
Author: User

JS How to remove an item from the specified index in an array:
There is a given function in the array object to delete the specified element in the arrays, although it is very useful, but the total feeling can not feel the more awkward, the following share a custom delete array to specify the index value of the elements of the function, I hope to give you a new idea.
The code example is as follows:

vararray=[]; array[0]= "Ant Tribe One"; array[1]= "Ant Tribe II"; array[2]= "Ant Tribe three"; array[3]= "Ant Tribe four"; array[4]= "Ant Tribe Five"; functionRemove (array,index) {if(Index<= (array.length-1))  {      for(vari=index;i<array.length;i++) {Array[i]=array[i+1]; }   }  Else  {     Throw NewError (' exceeds maximum index! ‘); } array.length=array.length-1; returnArray;} document.write (remove (array,2));

In the above code, declare a remove () function that has two parameters, the first parameter is an array object, the second parameter is to delete the index value of the array element, so the above code can delete the third element in the array, and output an array of deleted element users. The principle of implementation is also very simple, do a brief introduction:
The Remove () function first determines whether the index value passed in is approximately array.length-1 the maximum index value of the array, or throws an error if it is greater than, otherwise, the For loop starts with the index value I, sets the value of the array with the index value to the low i+1 item, and so on, The principle is generally so, if there are any questions can be posted message.

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=8931

For more information, refer to: http://www.softwhy.com/javascript/

JS How to remove an item from a specified index in an 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.