简体   繁体   English

Javascript-如何使用字符串作为参数返回数组?

[英]Javascript- How to return an array by with a string as parameter?

For a simple function, how can I return an array with a string as a parameter? 对于一个简单的函数,如何返回一个以字符串作为参数的数组? I want the array to split the string at the spaces so it returns as [" ", " "] . 我希望数组在空格处拆分字符串,使其返回为[" ", " "]

I tried using the split method split(" ", 2) but it didn't seem to work. 我尝试使用拆分方法split(" ", 2)但它似乎不起作用。

var arr = str.split(" ");

You don't need to add 2 after. 您之后不需要添加2。

The 2 limit the number of value in the array, if you want to split the string without a max, then take out the 2. 2限制数组中的值数,如果要分割不带max的字符串,则取出2。

The split will "split" the string with " ", if you want to split with other character, replace the space by what you need. 拆分将“拆分”字符串为“”,如果要与其他字符分开,请根据需要替换空格。

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

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