How to convert an integer into an array of digits

前端 未结 11 1078
别那么骄傲
别那么骄傲 2021-01-30 23:07

I want to convert an integer, say 12345, to an array like [1,2,3,4,5].

I have tried the below code, but is there a better way to do this?

11条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-30 23:21

    First, the integer is converted string & then to array. Using map function, individual strings are converted to numbers with the help of parseInt function. Finally, that array is returned as the result.

    const digitize = n => [...`${n}`].map(i => parseInt(i));
    

提交回复
热议问题