B
B
Banan442020-05-15 13:28:48
JavaScript
Banan44, 2020-05-15 13:28:48

How to get an array of digits from a number?

const array = 45
console.log(array.method(45))
// [4, 5]

No loops, just a method out of the box.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-05-15
@Banan44

[...`${num}`].map(Number)
or or or or or or or
num.toString().split('').map(n => +n)
Array.from(String(num), parseFloat)
('' + num).match(/./g).map(n => parseInt(n))
Object.values(num.toFixed()).map(n => n | 0)
[].map.call(/.+/.exec(num)[0], n => n * 1)
eval('['.concat(num, ']').replace(/\d/g, '$&,'))

Object.assign([], JSON.stringify(num)).map(JSON.parse)

or
Array(1 + (Math.log10(num) | 0)).fill().map((n, i) => (num / 10 ** i | 0) % 10).reverse()

or
((f = (x, a) => (a.unshift(x % 10), x = x / 10 | 0, x ? f(x, a) : a)) => f(num, []))()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question