Is it somehow possible to convert a string to an array as easy as possible without any hacks? and without splitting the string at some point?
For example bringing this:
temp = 'Text';
to this:
temp = ['Text'];
It might be a simple question but I couldn't find any solution without splitting the string.
asked Jan 22, 2021 at 17:46
user9024437
2 Answers 2
const temp = 'text';
console.log(new Array(temp));
console.log([temp]);
console.log(temp.split());
console.log([].concat(temp));
There are a few options out there.
answered Jan 22, 2021 at 17:54
Nicolae Maties
2,6782 gold badges18 silver badges29 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
If you want an array with the string as a single value just create a new array with that string.
temp = [temp];
answered Jan 22, 2021 at 17:53
Igor
62.5k10 gold badges111 silver badges181 bronze badges
Comments
lang-js
temp = [temp];? It is not really clear what you are asking...[ "T", "e", "x", "t" ]?