This is a great wa
Q:
How to read th
package cmd
impor
The present invent
/*
* Copyright (c
A new technique fo
The use of recombi
Practical, accurat
Q:
What is best p
Category: UncategoQ:
How do I convert a string of text into an array of string with JavaScript?
I have the following string:
const myText = "This is a test message about me, it goes on and on.";
I want to convert this to an array of strings. Something like:
[["This", "is", "a", "test", "message", "about", "me", "it", "goes", "on", "and", "on."]]
I've tried a few of my own methods and nothing seems to work. Does anyone know how to do this? Any help is appreciated.
A:
Here is one way you could do it. We split the string at each space, then iterate through each substring in the array. If we have an empty string, we move to the next one. If we don't, we append the current substring to the final result.
var text = "This is a test message about me, it goes on and on";
var result = [];
for (var index = 0; index < text.length; index++) {
if (text[index] == "") {
result.push(text.slice(index + 1, index + 1));
index += text[index + 1].length;
} else {
result.push(text.slice(index, index + text[index].length));
}
}
console.log(result);
A:
Array.prototype.map() can do the job.
const myText = "This is a test message about me, it goes on and on.";
const result = myText.split(" ").map(x => x.split("").join("")).join(",");
console.log(result);
A:
One line:
const myText = "This is a test message about me, it goes on and on.";
const result =
myText
.split(" ")
.map(item => item.split("").join(""))
.join(",")
.split(',')
console.log(result);
An iterative solution:
const myText = "This is a test message about me, it goes on and on.";
const result =
myText
.split(" ")
.map((item, index) => index >= 1 && item.length ? item.slice(0, item.length - 2) : item)
.join(",")
.split(',')
console.log(result);
And a single line solution that uses reduce:
const myText = "This is a test message about me, it goes on and on.";
const result =
myText
.split(" ")
.reduce((a, b) => a.concat(b.split("").slice(0, -2).join(",")), [])
.split(',')
console.log(result);
A:
You can split, slice and join it to an array.
let str = "This is a test message about me, it goes on and on."
let arr = []
for (let x of str.split(" ")) {
if (x.length < 1) {
arr.push(x.slice(0, -1))
}
}
let str = "This is a test message about me, it goes on and on."
let arr = []
for (let x of str.split(" ")) {
if (x.length < 1) {
arr.push(x.slice(0, -1))
}
}
console.log(arr);
Or you can use reduce
let str = "This is a test message about me, it goes on and on."
let arr = []
str.split(" ").reduce((res, x) => {
if (x.length < 1) {
arr.push(x.slice(0, -1))
return arr
}
return res.concat(x)
}, [])
console.log(arr)
And another version with arrow function and spread operator
let str = "This is a test message about me, it goes on and on."
let arr = []
str.split(" ").reduce((arr, x) => x.length < 1 ? arr.concat(x.slice(0, -1)) : arr.concat(x), [])
console.log(arr);
Or you can use an array slice.join(' ')
let str = "This is a test message about me, it goes on and on."
let arr = []
str.split(" ").reduce((arr, x) => x.length < 1 ? arr.concat(x.slice(0, -1)) : arr.concat(x.join(" ")), [])
console.log(arr);
More advanced version with array map method
let str = "This is a test message about me, it goes on and on."
let arr = str.split(" ").map(x => x.length < 1 ? x.slice(0, -1) : x)
console.log(arr);