Discussion on the deletion problem of the number of elements of the JavaScript

zhaozj2021-02-16  71

Var Arr = ['A', 'B', 'c']; To delete the 'b', there are two methods: 1. DELETE method: Delete Arr [1] This method of array is unchanged, this When Arr [1] turned to undefined, but it also benefited the index of the original array remained unchanged. At this time, it is necessary to traverse the array element to use for (index in arr) document.write ('Arr [' index '] = ' arr [index]); this traversal method skips the elements of Undefined

* This method IE4.o has supported it.

2. Array Object SPLICE Method: Arr.splice (1, 1); This method of array changes accordingly, but the original array index changes the first 1 in the splice parameter accordingly, is the start index of deletion (from 0) Since this, this is the second element of the array, which is the number of deletion elements. Only one element is deleted, ie, 'b'; this time the array element can be used in a normal traversal array, such as For , Because the deleted element is not retained in the array

* This method IE5.5 will only support

It is worth mentioning that the SPLICE method can be added to an array element while deleting an array element, such as Arr.SPLICE (1, 1, 'd', 'e'), D, and E two elements are added to the array arr The result array becomes Arr: 'a', 'd', 'e', ​​'c'

转载请注明原文地址:https://www.9cbs.com/read-23813.html

New Post(0)