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