Reputation: 11035
The current string of numbers is as follows:
["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"]
Notice that there are double quotes on both ends, which I need to get rid of.
The goal format is:
[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]....
I've tried using rStrip and replace, but that isn't working. Any ideas?
Thanks!
Upvotes: 0
Views: 89
Reputation: 16726
you've already got what you need, almost. this means it's easier to convert and parse than transform.
a really simple and fast way without looping:
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"]+"]")
Upvotes: 0
Reputation: 11983
Split, iterate, and cast:
var a = ["151,179,179,53"];
var values = a[0].split(',').map(Number);
console.log(values);
Upvotes: 1
Reputation: 576
You can use .split() and then iterate through the array, parsing the elements to ints.
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/>';
}
Upvotes: 0
Reputation: 1385
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);
}
Edit: That's exactly what @J148 suggested in his comment
Upvotes: 0