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>')
Run code snippetEdit code snippet Hide Results Copy to answer Expand
More simplified ES6 arrow function
var res = ['2', '10', '11'].map(v => parseInt(v, 10));
document.write('<pre>' + JSON.stringify(res, null, 3) + '<pre>')
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Or using Number
var res = ['2', '10', '11'].map(Number);
document.write('<pre>' + JSON.stringify(res, null, 3) + '<pre>')
Run code snippetEdit code snippet Hide Results Copy to answer Expand
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>')
Run code snippetEdit code snippet Hide Results Copy to answer Expand
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.
Answer from Pranav C Balan on Stack OverflowUse map() and parseInt()
var res = ['2', '10', '11'].map(function(v) {
return parseInt(v, 10);
});
document.write('<pre>' + JSON.stringify(res, null, 3) + '<pre>')
Run code snippetEdit code snippet Hide Results Copy to answer Expand
More simplified ES6 arrow function
var res = ['2', '10', '11'].map(v => parseInt(v, 10));
document.write('<pre>' + JSON.stringify(res, null, 3) + '<pre>')
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Or using Number
var res = ['2', '10', '11'].map(Number);
document.write('<pre>' + JSON.stringify(res, null, 3) + '<pre>')
Run code snippetEdit code snippet Hide Results Copy to answer Expand
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>')
Run code snippetEdit code snippet Hide Results Copy to answer Expand
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.
You can simply use the Number object.
ḷet res = ['2', '10', '11'].map(Number);
How to convert an array of string integers to an array of number integers
javascript - How to convert a string of numbers to an array of numbers? - Stack Overflow
javascript - Converting a array of string to array of Int in JS - Stack Overflow
javascript - convert array of strings into array of ints - Stack Overflow
I know this might be a silly question to ask but how could one covert the following into an array of numbers...
['1,2,6,8,15,16,17,20,22,23,24,25,26,27,30,32,33,34,36']
You can use Array.map to convert each element into a number.
var a = "1,2,3,4";
var b = a.split(',').map(function(item) {
return parseInt(item, 10);
});
Check the Docs
Or more elegantly as pointed out by User: thg435
var b = a.split(',').map(Number);
Where Number() would do the rest:check here
Note: For older browsers that don't support map, you can add an implementation yourself like:
Array.prototype.map = Array.prototype.map || function(_x) {
for(var o=[], i=0; i<this.length; i++) {
o[i] = _x(this[i]);
}
return o;
};
My 2 cents for golfers:
b="1,2,3,4".split`,`.map(x=>+x)
backquote is string litteral so we can omit the parenthesis (because of the nature of split function) but it is equivalent to split(','). The string is now an array, we just have to map each value with a function returning the integer of the string so x=>+x (which is even shorter than the Number function (5 chars instead of 6)) is equivalent to :
function(x){return parseInt(x,10)}// version from techfoobar
(x)=>{return parseInt(x)} // lambda are shorter and parseInt default is 10
(x)=>{return +x} // diff. with parseInt in SO but + is better in this case
x=>+x // no multiple args, just 1 function call
I hope it is a bit more clear.
Alternatively, you can one-line it using .map().
var arr = ["34", "23", "21"];
arr.map(Number);
>>> [34, 23, 21]
You can use javscript function parseInt() to acheive it
for example:-
var x=["1","2","3","4"]
x.forEach(function(item){
console.log(parseInt(x,10));
});
it will console all numeric values
ECMAScript5 provides a map method for Arrays, applying a function to all elements of an array.
Here is an example:
var a = ['1','2','3']
var result = a.map(function (x) {
return parseInt(x, 10);
});
console.log(result);
See Array.prototype.map()
You can do
var arrayOfNumbers = arrayOfStrings.map(Number);
- MDN Array.prototype.map
For older browsers which do not support Array.map, you can use Underscore
var arrayOfNumbers = _.map(arrayOfStrings, Number);
A quick one for modern browsers:
'14 2'.split(' ').map(Number);
// [14, 2]`
You can .split() to get an array of strings, then loop through to convert them to numbers, like this:
var myArray = "14 2".split(" ");
for(var i=0; i<myArray.length; i++) { myArray[i] = +myArray[i]; }
//use myArray, it's an array of numbers
The +myArray[i] is just a quick way to do the number conversion, if you're sure they're integers you can just do:
for(var i=0; i<myArray.length; i++) { myArray[i] = parseInt(myArray[i], 10); }
If the string = [1,1], then you can simply parse it as json. Like this:
const str = '[1,1]';
const resultArr = JSON.parse(str);
console.log(resultArr);
The result is an array of integers:
Array:
0: 1
1: 1
This is an example:
var arr = "[1, 1]".replace(/[\[\]]/g, "").split(",");
console.log(arr);
you can replace "[1, 1]" with you variable