gpt4 book ai didi

javascript - 将字符串中的坐标转换为 int 数组

转载 作者:行者123 更新时间:2023-11-29 21:28:09 25 4
gpt4 key购买 nike

我是 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

Read more about JSON.parse()

我不建议你使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com