JavaScript – Convert a Number into JS Array
You have a number, like 12345, and you need to convert it into an array where each element represents a digit of the number. For example, 12345 should become [1, 2, 3, 4, 5]. How can you achieve this in JavaScript?
In JavaScript, there are various ways to transform a number into an array of its digits. This can be useful in scenarios like performing calculations on each digit, validating number patterns, or just reformatting data for better usability.
These are the following methods by using these we can solve this problem:
1. Using Array.from() Method
The JavaScript Array.from() method creates an array from an object with a length property or an iterable. To use it, store a number in a variable and typecast it to a string as the first parameter of Array.from(). The second parameter is a callback function (e.g., myFunc), called for each character in the string. Inside the callback, convert the character back to an integer and return it. The resulting array contains the individual digits of the number.
let num = 235345;
// Getting the string as a parameter
// and typecasting it into an integer
let Func = num => Number(num);
let arr = Array.from(String(num), Func);
// Print the result array
console.log(arr);
Output
[ 2, 3, 5, 3, 4, 5 ]
2. Using mathematical operations
In this approach we use a while loop to iterate over the digit of a number. In each iteration of the loop, we use the modulo operator (%) to get the last digit of the number and add it to the beginning of the array using unshift(). We then update the number by dividing it by 10 and taking the floor value using Math.floor(). This process repeats until the number becomes 0.
let num = 12345;
let arr = [];
while (num > 0) {
arr.unshift(num % 10);
num = Math.floor(num / 10);
}
console.log(arr);
Output
[ 1, 2, 3, 4, 5 ]
3. Using a For Loop
The for loop approach involves converting the number to a string, iterating through each character of the string, and converting each character back to a number. These numbers are then pushed into an array, resulting in an array of the number’s digits.
const num = 12345;
const arr = [];
const str = String(num);
for (let i = 0; i < str.length; i++) {
arr.push(Number(str[i]));
}
console.log(arr);
Output
[ 1, 2, 3, 4, 5 ]
4. Using String.prototype.split() Method
Another approach to convert a number into an array in JavaScript is by using the split() method directly on the string representation of the number. This method involves converting the number to a string, splitting the string into an array of characters, and then converting each character back into a number.
const number = 235345;
let res = number.toString().split('').map(Number);
console.log(res);
Output
[ 2, 3, 5, 3, 4, 5 ]
5. Using Recursion
This approach uses recursion to break down the number into its individual digits and constructs the array step-by-step. The function calls itself with the remaining digits of the number until the number is reduced to zero.
function fun(num) {
if (num === 0) {
return [];
}
const dig = fun(Math.floor(num / 10));
dig.push(num % 10);
return dig;
}
const num = 235345;
const res = fun(num);
console.log(res);
Output
[ 2, 3, 5, 3, 4, 5 ]