How to convert a string of numbers to an array of numbers?

前端 未结 15 2056
死守一世寂寞
死守一世寂寞 2020-11-27 10:29

I have below string -

var a = \"1,2,3,4\";

when I do -

var b = a.split(\',\');

I get b as

15条回答
  •  攒了一身酷
    2020-11-27 10:48

    A more shorter solution: map and pass the arguments to Number:

    var a = "1,2,3,4";
    var b = a.split(',');
    console.log(b);
    var c = b.map(Number);
    console.log(c);

提交回复
热议问题