简体   繁体   English

将字符串转换为数组而不拆分它

[英]Convert a string to an array without splitting it

Is it somehow possible to convert a string to an array as easy as possible without any hacks?是否有可能在没有任何黑客攻击的情况下尽可能简单地将string转换为array and without splitting the string at some point?并且在某些时候不拆分string

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 .这可能是一个简单的问题,但如果不拆分string ,我找不到任何解决方案。

 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.有几个选择。

If you want an array with the string as a single value just create a new array with that string.如果您想要一个将字符串作为单个值的数组,只需使用该字符串创建一个新数组。

temp = [temp]; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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