How to convert an integer into an array of digits

前端 未结 11 1081
别那么骄傲
别那么骄傲 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:19

    I'd do this, to avoid using strings when you don't need them:

    var n = 12345;
    var arr = [];
    while(n>0){arr.unshift(n%10);n=n/10|0;}
    

提交回复
热议问题