3
let name = "Stack Overflow"

I want to ignore space and create an array with each alphabet as an element.

Expected result: ["S", "t", "a", "c", "k", "O", "v", "e", "r", "f", "l", "o", "w"]

Soham
  • 705
  • 3
  • 19

3 Answers3

4
console.log("Stack Overflow".replace(/\s/g, '').split(''));
Soham
  • 705
  • 3
  • 19
0

On javascript you're looking for spread (link to docs) operator associate with a filter method (link to docs):

The spread will create an array with all chars from your string, the filter will ignore all spaces for you:

let name = "Stack Overflow"

let myArray = [...name].filter(letter => letter !== ' ');

// myArray will be:
[
  'S', 't', 'a', 'c',
  'k', 'O', 'v', 'e',
  'r', 'f', 'l', 'o',
  'w'
]

This also right for es6 ! Cheer

Hamit YILDIRIM
  • 4,224
  • 1
  • 32
  • 35
Lucas Andrade
  • 4,315
  • 5
  • 29
  • 50
-1
name.replace(/ /g,'')

And then

Array.from(name)
Sid
  • 4,893
  • 14
  • 55
  • 110