Convert string with commas to array

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

    More "Try it Yourself" examples below.

    Definition and Usage The split() method is used to split a string into an array of substrings, and returns the new array.

    Tip: If an empty string ("") is used as the separator, the string is split between each character.

    Note: The split() method does not change the original string.

    var res = str.split(",");
    

提交回复
热议问题