2

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?

Khoerodin
  • 121
  • 3
  • 11

2 Answers2

12

Map it to the Number function:

var array = ["1", "2", "3", "4", "5"];
array = array.map(Number);
array; // [1, 2, 3, 4, 5]
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
6

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

The unary + acts more like parseFloat since it also accepts decimals.

Refer this

Try this snippet:

var array = ["1", "2", "3", "4", "5"];
array = array.map(function(item) {
  return +item;
});
console.log(array);
Community
  • 1
  • 1
Rayon
  • 36,219
  • 4
  • 49
  • 76