我是 javascript 初学者,在将字符串转换为 int 数组时遇到问题。 我发现这个案例
var string = "[1,2,3,4]";
但不适用于这个:
var string = "[[1,2],[1,2]]";
并且具有相同的语法
var result = [[1,2],[1,2]];
result = [[1,2],[1,2]];
最佳答案
试试这个:
var
str = '[[1,2],[3,4]]',
result;
result = JSON.parse(str);
result[0] // [1, 2]
result[1] // [3, 4]
result[0][0] // 1
result[1][1] // 4
我不建议您使用eval()
函数。
Object-Oriented JavaScript - Second Edition: eval() can be useful sometimes, but should be avoided if there are other options. Most of the time there are alternatives, and in most cases the alternatives are more elegant and easier to write and maintain. "Eval is evil" is a mantra you can often hear from seasoned JavaScript programmers. The drawbacks of using eval() are:
Security – JavaScript is powerful, which also means it can cause damage. If you don't trust the source of the input you pass to eval(), just don't use it.
Performance – It's slower to evaluate "live" code than to have the code directly in the script.
关于javascript - 将字符串中的坐标转换为 int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37005547/