How to convert comma separated string into numeric array in javascript

后端 未结 9 1947
粉色の甜心
粉色の甜心 2021-02-05 02:06

I have a one-dimensional array of integer in JavaScript that I\'d like to add data from comma separated string, Is there a simple way to do this?

e.g : var strVale

9条回答
  •  Happy的楠姐
    2021-02-05 02:58

    ? "123,87,65".split(",").map(Number)
    > [123, 87, 65]
    

    Edit >>

    Thanks to @NickN & @connexo remarks! A filter is applicable if you by eg. want to exclude any non-numeric values:

    ?", ,0,,6, 45,x78,94c".split(",").filter(x => x.trim().length && !isNaN(x)).map(Number)
    > [0, 6, 45]
    

提交回复
热议问题