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

前端 未结 15 2066
死守一世寂寞
死守一世寂寞 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 11:07

    You can use JSON.parse, adding brakets to format Array

    const a = "1,2,3,4";
    const myArray = JSON.parse(`[${a}]`)
    console.log(myArray)
    console.info('pos 2 = ',  myArray[2])

提交回复
热议问题