How to Split a Number into an Array in JavaScript
Split a number into an Array using toString().split() in JavaScript
One way to split a number into an array of its individual digits in JavaScript is to use the toString()
method to convert the number to a string, then use the split()
method to create an array of characters. Finally, use the map()
method to convert each character back to a number. Here’s an example:
1 2 3 4 5 6 7 8 9 |
const num = 12345; const numArray = num.toString().split('').map(function(item) { return parseInt(item, 10); }); console.log(numArray); // [1, 2, 3, 4, 5] |
In this example,
1 2 3 4 5 |
const num = 12345; const numArray = num.toString() |
num.toString()
converts the number 12345
to a string "12345"
.
1 2 3 4 5 6 7 |
const num = 12345; const numArray = num.toString().split('') console.log(numArray); // ['1', '2', '3', '4', '5'] |
The split('')
method splits the string into an array of characters ["1", "2", "3", "4", "5"]
. Finally, the map()
method converts each character back to a number.
You can also use arrow function syntax instead of a regular function:
1 2 3 |
const numArray = num.toString().split('').map(item => parseInt(item, 10)); |
When splitting a number into an array of its individual digits in JavaScript, it may be useful to define a reusable function for this purpose. This can improve the readability and maintainability of your code by making the process more explicit and reducing duplication.
Here’s an example of a reusable function that splits a number into an array of its digits:
1 2 3 4 5 6 7 8 9 10 11 12 |
const numArray = (num) => { return String(num) .split("") .map((item) => parseInt(item, 10)); } console.log(numArray(1234)); // 👉️ [ 1, 2, 3, 4 ] console.log(numArray(123)); // 👉️ [ 1, 2, 3 ] console.log(numArray(12)); // 👉️ [ 1, 2 ] console.log(numArray(1)); // 👉️ [ 1 ] |
In this example, the numArray
function takes a number as an argument and returns an array of its individual digits. The function uses the same approach as described earlier, converting the number to a string, splitting it into an array of characters, and then converting each character back to a number. By defining this as a reusable function, it can be easily used in multiple places throughout your code.
Read: How to Convert a Map to an Array in JavaScript
Using Array.from() Method
To split a number into an array of its individual digits in JavaScript, you can use the Array.from()
method. This method creates a new, shallow-copied Array instance from an array-like or iterable object.
To split a number, first, convert it to a string using String(num)
and pass the result as the first argument to Array.from()
. The second argument is the Number
constructor, which is used to convert each character in the string to a number.
Here’s an example:
1 2 3 4 5 |
const num = 12345; const numArray = Array.from(String(num), Number); console.log(numArray); // [1, 2, 3, 4, 5] |
In this example, String(num)
converts the number 12345
to a string "12345"
, and Array.from(String(num), Number)
creates an array of numbers [1, 2, 3, 4, 5]
from the characters in the string.