I want to convert an integer, say 12345, to an array like [1,2,3,4,5].
12345
[1,2,3,4,5]
I have tried the below code, but is there a better way to do this?
The solutions proposed work fine in most cases but not with negative numbers.
There is a solution for such cases.
const intToArray = (int) => { return String(int).match(/-?\d/g).map(Number) } console.log(intToArray(312)) // [3, 1, 2] console.log(intToArray(-312)) // [-3, 1, 2]