151
rated 0 times
[
155]
[
4]
/ answers: 1 / hits: 126585
/ 14 Years ago, sat, november 27, 2010, 12:00:00
I want to convert the following string '14 2'
into an array of two integers.
How can I do it ?
More From » javascript
I want to convert the following string '14 2'
into an array of two integers.
How can I do it ?
You can .split()
to get an array of strings, then loop through to convert them to numbers, like this:
var myArray = 14 2.split( );
for(var i=0; i<myArray.length; i++) { myArray[i] = +myArray[i]; }
//use myArray, it's an array of numbers
The +myArray[i]
is just a quick way to do the number conversion, if you're sure they're integers you can just do:
for(var i=0; i<myArray.length; i++) { myArray[i] = parseInt(myArray[i], 10); }