Convert javascript string array to integer javascript array

I have an array,

var array = ["1","2","3","4","5"]; 

then i need to convert to

 var array = [1,2,3,4,5]; 

How can i convert?

+2
source share
2 answers

Match it with the Number function:

 var array = ["1", "2", "3", "4", "5"]; array = array.map(Number); array; // [1, 2, 3, 4, 5] 
+12
source

The map () method creates a new array with the results of calling the provided function for each element in this array.

Unary + acts more like parseFloat, since it also accepts decimal numbers.

Contact

Try this snippet:

 var array = ["1", "2", "3", "4", "5"]; array = array.map(function(item) { return +item; }); console.log(array); 
+6
source

All Articles