javascript - convert an array of strings and numbers to an array of numbers -
i have array of strings , numbers. need convert items numbers, i.e :
var = ["42.00", 33, 12.02, "30", 10];
convert to:
[42.00, 33, 12.02, 30, 10]
for fallback compatibily purpose:
var = ["42.00", 33, 12.02, "30", 10]; var out; if (typeof(array.prototype.map == 'function')) { out = a.map(function(el){ return typeof(el) !== 'number' ? parsefloat(el) : el }); } else { out = []; (var = 0; < a.length; ++i) { out[i] = typeof(a[i]) !== 'number' ? parsefloat(a[i]) : a[i]; } } console.log(out);
if element number won't parse it, else will.
if .map function not defined, procede using regular loop. should done because browsers not support ecma functions (like ie9):
fiddle :: http://jsfiddle.net/kndgcty8/1/
Comments
Post a Comment