简体   繁体   中英

Add whitespace to an array in javascript

Simply put, I want to split a string in javascript into an array, which should also include all the whitespaces in the original string.

Ex: "Hi There" should be broken down to ['H','i',' ','T','h','e','r','e'] I tried using str.split('') only to find the results as ['H','i','','T','h','e','r','e'] I want a whitespace after "i" in "Hi" here in this example

Try this:

 const result = "Hi There".split(""), solution = [..."Hi There"]; console.log(result); console.log(solution);

I tried the same as yours, and it worked, ie "Hi There".split('') gives a space where it finds one:

 console.log("Hi There".split(''))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM