开发者

Converting an array of string into integer using jquery

I am tryin开发者_StackOverflowg 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);
});


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]


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


var cdata = data.values.split(",");
$.map( cdata, function(i, l){
   return +l;
});

Without jQuery (using the browsers native map method):

"1,2,3,4,5,6".split(',').map(function(e) {return +e});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜