Home > Web Front-end > Front-end Q&A > How to convert string to array in es6

How to convert string to array in es6

青灯夜游
Release: 2023-01-11 09:19:16
Original
4972 people have browsed it

es6 Method to convert a string into an array: 1. Use the extension operator "...", the syntax "[..."string"]"; 2. Use the extension method of the array from( ), syntax "Array.from("string")".

How to convert string to array in es6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

es6 Method of converting string to array

Method 1:

Use ES6 array extension. The spread operator (spread) is three dots (...). It is like the inverse operation of the rest parameter, converting an array into a comma-separated parameter sequence.

[..."abcdefg1234567"]
Copy after login

Result:

Method 2:

In es6, you can use the array extension method Array .from() converts a string into an array. The Array.from method is used to convert two types of objects into real arrays: array-like objects and iterables. Objects (including ES6's new data structures Set and Map).

Array.from("hello")
Copy after login

Result:

[Recommended learning: javascript advanced tutorial

The above is the detailed content of How to convert string to array in es6. For more information, please follow other related articles on the PHP Chinese website!

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