For simple array members like that, you can use JSON.parse
.
(对于像这样的简单数组成员,您可以使用JSON.parse
。)
var array = JSON.parse("[" + string + "]");
This gives you an Array of numbers.
(这会给你一个数字数组。)
[0, 1]
If you use .split()
, you'll end up with an Array of strings.
(如果你使用.split()
,你最终会得到一个字符串数组。)
["0", "1"]
Just be aware that JSON.parse
will limit you to the supported data types.
(请注意, JSON.parse
会限制您使用支持的数据类型。)
If you need values like undefined
or functions, you'd need to use eval()
, or a JavaScript parser.(如果您需要undefined
或函数等值,则需要使用eval()
或JavaScript解析器。)
If you want to use .split()
, but you also want an Array of Numbers, you could use Array.prototype.map
, though you'd need to shim it for IE8 and lower or just write a traditional loop.
(如果你想使用.split()
,但你也想要一个数字数组,你可以使用Array.prototype.map
,虽然你需要为IE8填充它并降低或者只是写一个传统的循环。)
var array = string.split(",").map(Number);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…