54

In JS if you would like to split user entry into an array what is the best way of going about it?

For example:

entry = prompt("Enter your name")

for (i=0; i<entry.length; i++)
{
entryArray[i] = entry.charAt([i]);
}

// entryArray=['j', 'e', 'a', 'n', 's', 'y'] after loop

Perhaps I'm going about this the wrong way - would appreciate any help!

Captain Sparrow
  • 1,064
  • 15
  • 25
methuselah
  • 11,964
  • 41
  • 148
  • 269

10 Answers10

90

Use the .split() method. When specifying an empty string as the separator, the split() method will return an array with one element per character.

entry = prompt("Enter your name")
entryArray = entry.split("");
James Hill
  • 58,309
  • 18
  • 142
  • 160
16

ES6 :

const array = [...entry]; // entry="i am" => array=["i"," ","a","m"]
Community
  • 1
  • 1
Abdennour TOUMI
  • 76,783
  • 33
  • 229
  • 231
11

use var array = entry.split("");

Jordan Wallwork
  • 3,026
  • 2
  • 23
  • 47
10

Do you care for non-English names? If so, all of the presented solutions (.split(''), [...str], Array.from(str), etc.) may give bad results, depending on language:

"प्रणव मुखर्जी".split("") // the current president of India, Pranab Mukherjee
// returns ["प", "्", "र", "ण", "व", " ", "म", "ु", "ख", "र", "्", "ज", "ी"]
// but should return ["प्", "र", "ण", "व", " ", "मु", "ख", "र्", "जी"]

Consider using the grapheme-splitter library for a clean standards-based split: https://github.com/orling/grapheme-splitter

Orlin Georgiev
  • 1,301
  • 15
  • 18
5
var foo = 'somestring'; 

// bad example https://stackoverflow.com/questions/6484670/how-do-i-split-a-string-into-an-array-of-characters/38901550#38901550

var arr = foo.split(''); 
console.log(arr); // ["s", "o", "m", "e", "s", "t", "r", "i", "n", "g"]

// good example
var arr = Array.from(foo);
console.log(arr); // ["s", "o", "m", "e", "s", "t", "r", "i", "n", "g"]

// best
var arr = [...foo]
console.log(arr); // ["s", "o", "m", "e", "s", "t", "r", "i", "n", "g"]
аlex dykyі
  • 4,877
  • 25
  • 38
3

...and also for those who like literature in CS.

array = Array.from(entry);
Redu
  • 22,595
  • 5
  • 50
  • 67
3

Use split method:

entry = prompt("Enter your name");
entryArray = entry.split("");

Refer String.prototype.split() for more info.

user229044
  • 222,134
  • 40
  • 319
  • 330
Lukman
  • 17,846
  • 5
  • 53
  • 63
3

You can try this:

var entryArray = Array.prototype.slice.call(entry)

Mic
  • 24,186
  • 9
  • 56
  • 69
  • 2
    See [http://jsperf.com/string-to-array-of-characters](http://jsperf.com/string-to-array-of-characters). – XP1 Feb 11 '12 at 22:06
2

ES6 is quite powerful in iterating through objects (strings, Array, Map, Set). Let's use a Spread Operator to solve this.

entry = prompt("Enter your name");
var count = [...entry];
console.log(count);
JideLambo
  • 71
  • 3
1

You can try this way:

let entry = prompt("Enter your name") 
let entryArray = entry.split('')
console.log(entryArray)

here is fiddle https://jsfiddle.net/swapanil/Lp1arvqc/17/