Created
December 27, 2018 16:10
-
-
Save caio-ribeiro-pereira/08b7b7f0694dbe94d3192932e3d36687 to your computer and use it in GitHub Desktop.
Três maneiras de converter string para array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usando o clássico método String.prototype.split() | |
const title = 'Book'; | |
// É necessário string vazia em argumento | |
console.log(title.split('')); // ['B', 'o', 'o', 'k'] | |
// split sem argumento vai retornar um array com uma única string | |
console.log(title.split()); // ['Book'] | |
// Usando Array.from() | |
const title = 'Book'; | |
console.log(Array.from(title)); // ['B', 'o', 'o', 'k'] | |
// Combinando array com spread operador | |
const title = 'Book'; | |
console.log([...title]); // ['B', 'o', 'o', 'k'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment