Skip to main content
added 74 characters in body
Source Link
PDub
  • 11
  • 4

Here's an alternative to Nicolás Fantone'sNicolás Fantone's answer. You could argue it's maybe a little less readable. The emphasis is that Array.from() can take an optional map function as a parameter. There are some performance gains this way since no intermediate array gets created.

const n = 123456; Array.from(n.toString(), (val) => Number(val)); // [1, 2, 3, 4, 5, 6] 

Here's an alternative to Nicolás Fantone's answer. You could argue it's maybe a little less readable. The emphasis is that Array.from() can take an optional map function as a parameter. There are some performance gains this way since no intermediate array gets created.

const n = 123456; Array.from(n.toString(), (val) => Number(val)); // [1, 2, 3, 4, 5, 6] 

Here's an alternative to Nicolás Fantone's answer. You could argue it's maybe a little less readable. The emphasis is that Array.from() can take an optional map function as a parameter. There are some performance gains this way since no intermediate array gets created.

const n = 123456; Array.from(n.toString(), (val) => Number(val)); // [1, 2, 3, 4, 5, 6] 
Source Link
PDub
  • 11
  • 4

Here's an alternative to Nicolás Fantone's answer. You could argue it's maybe a little less readable. The emphasis is that Array.from() can take an optional map function as a parameter. There are some performance gains this way since no intermediate array gets created.

const n = 123456; Array.from(n.toString(), (val) => Number(val)); // [1, 2, 3, 4, 5, 6]