Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use es6 to convert a string to an array

Shulou Source: shulou.com Published: 2022-06-03 16:05:32 10月03日 Update

This article will explain in detail how to use es6 to convert strings into arrays. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article. Es6's method of converting a string into an array: 1. Using the extension operator "...", syntax "[..." The string "]"; 2. Using the array extension method from (), the syntax "Array.from (" string ")".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

The method of converting es6 string to Array

Method 1:

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

[... "abcdefg1234567"]

Results:

Method 2:

In es6, you can use the array extension method Array.from () to convert strings into arrays, and the Array.from method is used to turn two types of objects into real arrays: array-like objects (array-like object) and iterable objects (including ES6's new data structures Set and Map).

Array.from ("hello")

Results:

On how to use es6 to convert strings into arrays to share here, I hope the above can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Arrays characters strings methods objects contents parameters articles more knowledge articles results syntax operators operations yes three sequences tutorials data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker MariaDB MySQL macOS