Convert string with commas to array

后端 未结 18 1052
执念已碎
执念已碎 2020-11-22 12:08

How can I convert a string to a JavaScript array?

Look at the code:

var string = \"0,1\";
var array = [string];
alert(array[0]);

In

18条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 12:28

    This is easily achieved in ES6;

    You can convert strings to Arrays with Array.from('string');

    Array.from("01")
    

    will console.log

    ['0', '1']
    

    Which is exactly what you're looking for.

提交回复
热议问题