[Home] >  Number >  Get the arrays of digits from a number

Get the arrays of digits from a number

JavaScript version

const digitize = (n) => `${n}`.split('').map((v) => parseInt(v, 10));

// Or
const digitize = (n) => [...`${n}`].map((v) => parseInt(v, 10));

TypeScript version

const digitize = (n: number): number[] => `${n}`.split('').map((v) => parseInt(v, 10));

// Or
const digitize = (n: number): number[] => [...`${n}`].map((v) => parseInt(v, 10));

Examples

digitize(123); // [1, 2, 3]