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"]

4
  • I would try with name.split('').filter(e => e !== ' ') or maybe with name.replace(' ', '').split(''). Commented May 14, 2020 at 15:16
  • 2
    This is duplicated by stackoverflow.com/questions/13272406/… Commented May 14, 2020 at 15:20
  • 3
    What is expected result? I though its ["S", "t", "a", "c", "k", "O", "v", "e", "r", "f", "l", "o", "w"] Commented May 14, 2020 at 15:24
  • @Soham yes you are right Commented May 14, 2020 at 15:29

3 Answers 3

4
console.log("Stack Overflow".replace(/\s/g, '').split(''));
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

I want to ignore space and create an array with each alphabet
-1
name.replace(/ /g,'')

And then

Array.from(name)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.