Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an array of string into integer using jquery

I am trying to convert an array of strings into an array of integers in jquery.

Here is my attempt:

var cdata = data.values.split(",");
$.each( cdata, function(i, l){
   l = parseInt(l);
});
like image 402
user559142 Avatar asked Aug 22 '11 00:08

user559142


2 Answers

I think that you not need use Jquery for this case. In javascript pure:

var str = "1,2,3";
var ArrayOfInts = str.split(',').map(Number); //Output: [1,2,3]

like image 59
Kakashi Avatar answered Oct 13 '22 06:10

Kakashi


// Use jQuery
$('.usesJQuery');

// Do what you want to acomplish with a plain old Javascript loop
var cdata = data.values.split(",");
for(var i = 0; i < cdata.length; i++)
    cdata[i] = parseInt(cdata[i], 10);
like image 8
Paul Avatar answered Oct 13 '22 06:10

Paul