What are the methods of deleting array elements in javascript
This article mainly explains "what are the methods of deleting array elements in javascript". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the methods of deleting array elements in javascript".
1.delete method: delete arr [1]
In this way, the length of the array remains the same, and arr [1] becomes undefined, but it also has the advantage that the index of the original array remains the same, and you can only use it if you want to traverse the array elements.
For (index in arr)
[xss_clean] ('arr [' + index+'] ='+ arr [index])
This traversal skips the elements of undefined
* this method will be supported by IE4.o later.
two。 Splice method of array object: arr.splice (1 ~ 1)
In this way, the length of the array changes accordingly, but the original array index also changes accordingly.
The first 1 in the splice parameter, which is the starting index of the deletion (from 0), in this case the second element of the array
The second 1, which is the number of deleted elements, deletes only one element, that is,'b'.
At this point, traversing array elements can be done in a normal way, such as for, because the deleted element is in the
Is not reserved in the array
* this method will not be supported until later on IE5.5
It is worth mentioning that while deleting array elements, the splice method can also add array elements.
For example, arr.splice is added to the array arr, and the two elements of dline e are added to the array
The resulting array becomes arr:'a','d','e','c'
Thank you for your reading, the above is the content of "what are the methods of deleting array elements in javascript". After the study of this article, I believe you have a deeper understanding of what the method of deleting array elements in javascript has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!