Home > Web Front-end > JS Tutorial > A small example of splitting a string into an array through the split function in js_javascript skills

A small example of splitting a string into an array through the split function in js_javascript skills

WBOY
Release: 2016-05-16 17:21:58
Original
1416 people have browsed it

Copy code The code is as follows:



Usage of split in JS

Copy code The code is as follows:





split








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

is not retained in the array

* 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'

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template