JS 文本转数组
我有这样的文字:
2/92/20
3/32/32
4/62/6
5/22/28
6/60/61
7/33/32
8/34/31
9/31/19
10/19/19
11/34/39
12/32/32
14/19/25
15/45/37
16/32/32
17/84/36
18/72/33
我需要它是这样的:
// 2/92/20
chars[0][0]=2;
chars[0][1]=92;
chars[0][2]=20;
我该怎么做? PS 分割必须位于:
$.ajax({
type: "POST",
url: "char_info2.php",
dataType: "html",
success: function(data)
{
//here
}
});
I have this text:
2/92/20
3/32/32
4/62/6
5/22/28
6/60/61
7/33/32
8/34/31
9/31/19
10/19/19
11/34/39
12/32/32
14/19/25
15/45/37
16/32/32
17/84/36
18/72/33
And I need it to be like:
// 2/92/20
chars[0][0]=2;
chars[0][1]=92;
chars[0][2]=20;
How can I do that? P.S. The split must be in:
$.ajax({
type: "POST",
url: "char_info2.php",
dataType: "html",
success: function(data)
{
//here
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有一个带有换行符的字符串,则可以使用
split< /code>
:
DEMO
如果你想拥有它们作为整数,您将不得不再次循环它们。
但我同意这些评论,将它们作为 JSON 发送更有意义!
If you have one string with line breaks, you can use
split
:DEMO
If you want to have them as integers you would have to loop over them again.
But I agree with the comments, sending them as JSON makes much more sense!
您可以使用 split 或正则表达式:
You can either use split or a regular expression:
您可以在换行符和斜杠上分割
http://www.w3schools.com/jsref/jsref_split.asp< /a>
You could split on newline and slash
http://www.w3schools.com/jsref/jsref_split.asp