Transform variable into array

1

I have a variable that calls all people names.

var nome = "João Miguel Pedro";

How do I make it to become the following array:

var nome1 = ["João","Miguel","Pedro"];
    
asked by anonymous 14.08.2017 / 20:09

1 answer

5

First of all, one option is to use split() to separate by space. See:

var nome = "João Miguel Pedro"

var nome1 = nome.split(" ");

console.log(nome1);

Or you can do a regex to make a split() in all capital letters, as you queried in the comments. See:

var nome = "JoãoMiguelPedro"

var nome1 = nome.split(/(?=[A-Z])/);

console.log(nome1);
    
14.08.2017 / 20:13