Convert string with commas to array

后端 未结 18 1167
执念已碎
执念已碎 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:21

    You can use javascript Spread Syntax to convert string to an array. In the solution below, I remove the comma then convert the string to an array.

    var string = "0,1"
    var array = [...string.replace(',', '')]
    console.log(array[0])
    

提交回复
热议问题