[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

split string to array of float is different in JavaScript and jQuerry

Patrick Chung

4/22/2015 12:18:00 PM


Sometimes it is practical to store array of numbers into string, and to separate values by comma for example. To collect this data from text box in our form, we use different code in JavaScript and jQuery.

Read more:

http://www.cirvirlab.com/index.php/jquery-code-examples/185-jquery-javascript-split-string-to-array-of-...
1 Answer

Christoph M. Becker

4/22/2015 12:47:00 PM

0

Patrick Chung wrote:

> Sometimes it is practical to store array of numbers into string, and to separate values by comma for example. To collect this data from text box in our form, we use different code in JavaScript and jQuery.
>
> Read more:
>
> http://www.cirvirlab.com/index.php/jquery-code-examples/185-jquery-javascript-split-string-to-array-of-...

Consider using $.map instead of $.each:

var cosPoints = $('#textfield1').val().split(',');
var arrayPoints = $.map(cosPoints, function (str) {
return parseFloat(str);
});

Or, using plain ES 5.1:

var arrayPoints = cosPoints.map(function (str) {
return parseFloat(str);
});

--
Christoph M. Becker