1

I need to convert a string with multiple numbers to an array in jQuery. My string is the one below:

var strVal = "901,235,342,421,345,745,544,324,445,123,232,986,345,678";
user7393973
  • 2,270
  • 1
  • 20
  • 58
ziel
  • 127
  • 2
  • 11

2 Answers2

2

You don't need jquery for this

var strVal = "901,235,342,421,345,745,544,324,445,123,232,986,345,678";
var result = strVal.split(",").map(x=>+x);
console.log(result);
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30
2

You could use String.split with ',' and then Array#map with to Number converted values

var strVal = "901,235,342,421,345,745,544,324,445,123,232,986,345,678",
    numbers = strVal.split(',').map(Number);

console.log(numbers);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392