当前的数字字符串如下:
["151,47,47,200,42,130,39,39,152,144,84,66,137,39,83,155,40,49,109,178,91,196,99,190,36,38,169,63,31,60,83,61,79,156,137,64,169,173,40,39,87,188,170,154,188,152,71,106,76,105,184,122,60,71,134,32,39,48,54,77,47,168,134,60,59,161,95,167,108,115,74,132,143,197,99,193,96,174,182,61,48,129,59,190,76,194,197,54,61,72,145,193,70,192,106,164,52,179,179,53"]
请注意,两端都有双引号,我需要摆脱它。
目标格式为:
[151,47,47,200,42,130,39,39,152,144,84,66,137,39,83,155,40,49,109,178,91,196,99,190,36,38,169,63,31,60]....
我尝试过使用rStrip并替换,但这不起作用。有任何想法吗?
谢谢!
答案 0 :(得分:1)
拆分,迭代和施放:
var a = ["151,179,179,53"];
var values = a[0].split(',').map(Number);
console.log(values);
答案 1 :(得分:0)
var strings = data[0].split(','); // array of strings containing numbers
var numbers = [];
for (var i = 0; i < strings.length; i++) {
numbers[i] = parseInt(strings[i],10);
}
编辑:这正是@ J148在评论中建议的内容
答案 2 :(得分:0)
您可以使用.split()然后遍历数组,将元素解析为整数。
HTML:
<div id='show'></div>
JS:
var stringArray = "151,47,47,200,42,130,39,39,152,144,84,66,137,39,83,155,40,49,109,178,91,196,99,190,36,38,169,63,31,60,83,61,79,156,137,64,169,173,40,39,87,188,170,154,188,152,71,106,76,105,184,122,60,71,134,32,39,48,54,77,47,168,134,60,59,161,95,167,108,115,74,132,143,197,99,193,96,174,182,61,48,129,59,190,76,194,197,54,61,72,145,193,70,192,106,164,52,179,179,53";
var numArray = stringArray.split(',');
for (var n=0;n<numArray.length;n++) {
numArray[n] = parseInt(numArray[n],10);
}
//add 1 to each to prove they are ints
for (var n=0;n<numArray.length;n++) {
document.getElementById('show').innerHTML += numArray[n]+'+1 = '+(numArray[n]+1)+'<br/>';
}
答案 3 :(得分:0)
你几乎已经得到了你所需要的东西。 这意味着转换和解析比转换更容易。
一种非常简单快速的方式,无需循环:
JSON.parse("["+["151,47,47,200,42,130,39,39,152,144,84,66,137,39,83,155,40,49,109,178,91,196,99,190,36,38,169,63,31,60,83,61,79,156,137,64,169,173,40,39,87,188,170,154,188,152,71,106,76,105,184,122,60,71,134,32,39,48,54,77,47,168,134,60,59,161,95,167,108,115,74,132,143,197,99,193,96,174,182,61,48,129,59,190,76,194,197,54,61,72,145,193,70,192,106,164,52,179,179,53"]+"]")