Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
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

 Answers
7

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); } 

[#94825] Thursday, November 25, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clarkulisesa

Total Points: 422
Total Questions: 93
Total Answers: 112

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
clarkulisesa questions
Mon, Feb 24, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;