convert string array to integer array
You can simply use the Number object.
ḷet res = ['2', '10', '11'].map(Number);
Use map()
and parseInt()
var res = ['2', '10', '11'].map(function(v) {
return parseInt(v, 10);
});
document.write('<pre>' + JSON.stringify(res, null, 3) + '<pre>')
More simplified ES6 arrow function
var res = ['2', '10', '11'].map(v => parseInt(v, 10));
document.write('<pre>' + JSON.stringify(res, null, 3) + '<pre>')
Or using Number
var res = ['2', '10', '11'].map(Number);
document.write('<pre>' + JSON.stringify(res, null, 3) + '<pre>')
Or adding
+
symbol will be much simpler idea which parse the string
var res = ['2', '10', '11'].map(v => +v );
document.write('<pre>' + JSON.stringify(res, null, 3) + '<pre>')
FYI : As @Reddy comment -
map()
will not work in older browsers either you need to implement it ( Fixing JavaScript Array functions in Internet Explorer (indexOf, forEach, etc.) ) or simply use for loop and update the array.
Also there is some other method which is present in it's documentation please look at Polyfill , thanks to @RayonDabre for pointing out.