How do I separate an integer into separate digits in an array in JavaScript?
Why not just do this?
var n = 123456789;
var digits = (""+n).split("");
What about:
const n = 123456;
Array.from(n.toString()).map(Number);
// [1, 2, 3, 4, 5, 6]
(123456789).toString(10).split("")
^^ this will return an array of strings
(123456789).toString(10).split("").map(function(t){return parseInt(t)})
^^ this will return an array of ints
I realize this was asked several months ago, but I have an addition to samccone's answer which is more succinct but I don't have the rep to add as a comment!
Instead of:
(123456789).toString(10).split("").map(function(t){return parseInt(t)})
Consider:
(123456789).toString(10).split("").map(Number)