Home >Web Front-end >JS Tutorial >A small example of splitting a string into an array through the split function in js_javascript skills
Usage of split in JS
JS delete array elements
var arr=['a','b','c'];
To remove the 'b', there are two methods:
1.delete method: delete arr[1]
In this way, the length of the array remains unchanged. At this time, arr[1] becomes undefined, but it also has the advantage that the index of the original array remains unchanged. At this time, you can use
to traverse the array elements.
for(index in arr)
document.write('arr[' index ']=' arr[index]);
This traversal method skips undefined elements
* This method will be supported by IE4.o from now on
2. Array object splice method: arr.splice(1,1);
In 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 deletion (counting from 0), here it is the second element of the array
The second 1 is the number of deleted elements. Only one element is deleted here, namely 'b';
At this time, you can traverse the array elements in the normal way of traversing the array, such as for, because the deleted element is in
* This method is only supported after IE5.5
It is worth mentioning that while the splice method deletes array elements, it can also add new array elements
For example, arr.splice(1,1,'d','e'), the two elements d and e are added to the array arr
The resulting array becomes arr:'a','d','e','c'