How to convert number to array in ES6 JavaScript

In this post, we are going to learn how to convert numbers to arrays in ES6 JavaScript for example. We will convert a positive or negative number into an array by using the ES6 array.from() method,spread operator and array.map().

1. Simple way to convert number to array Javascript


In this Javascript program example we are running a while loop until the number is greater than zero and inside the loop finding and pushing the module of each digit in a given number into the result array. We have reversed the result array to get the array of numbers. Let us understand with the below example.

var number = 111369;


const numToarr = (number) => {
    var resArr = [];
    while(number>0) { resArr.push(number%10); number = number/10|0; }
    return resArr.reverse();
}

console.log('number to array:\n',numToarr(number)) 

Output

number to array:
 [ 1, 1, 1, 3, 6, 9 ]

Array.from() to Convert number to array in ES6 JavaScript


The Javascript ES6 array.from() is an static method that create an array object from array=like or iterable objects(string,array,map,set).

Syntax

Array.from(arraylike,mapfn,thisArg)

Parameters

  • array-like: It is an array-like or iterable object that we want to convert into an array.
  • mapfn: This is optional and represents a mapfn call on every element of the array.
  • thisarg : value to use this when executing mapfn.

JavaScript program to convert numbers to array


In this example, we have used the array.from() method to convert number to the array, First, we have typecast given number to string . Second, defined a callback function(callbackFun) that typecast every digit of a given number string to a number by using the numbet() function. The array.from() method will create a new array instance and return an array of numbers.

var number = 111369;


 #array.from() to convert number to array in ES6 JavaScript 

let callbackFun = num => Number(num);
  
var numtoArr = Array.from(String(number), callbackFun);
  

console.log('number to array:',numtoArr);


Output

number to array: [ 1, 1, 1, 3, 6, 9 ]

2. array.map() to convert number to array in ES6 JavaScript


The array.map() method is iterated over the element of the array and modifies elements of the array by applying some operation and returning the modified element as a new array.

In this javascript program, we have converted the given number to a string and split it using space. The array.map() function iterates over each digit of the number that is typecast to the number and It will return an array of numbers

//JavaScriprt program to convert number to array in ES6 JavaScript

var number = 111369;


var numtoArr = String(number).split("").map((number)=>{
  return Number(number)
})
  
console.log('number to array:',numtoArr);

Output

number to array: [ 1, 1, 1, 3, 6, 9 ]

Use this code if the number contains a decimal.

var numtoArr = String(number).replace(/\D/g, '0').split("").map((number)=>{
  return Number(number)
})

3. array. map() to convert number to array in ES6 JavaScript


This Javascript program can convert both positive or negative numbers to the array, Firstly We have converted the given number to a string and the string.match() method to match a given string against a regular expression.The map() function runs for each digit of a given number and convert them to the number and return an array of number.

var number = 111369;
var negNum = -111369;

const numToArr = (num) => {
  return String(num).match(/-?\d/g).map(Number)
}

console.log('number to array:\n',numToArr(number)) 
console.log('neagtive number to array :\n',numToArr(negNum)) 

Output

number to array:
 [ 1, 1, 1, 3, 6, 9 ]
neagtive number to array :
 [ -1, 1, 1, 3, 6, 9 ]

4. Spread operator to convert number to array in ES6 JavaScript


The ES6 spread operator takes an iterate object(Array, Set, Map, String) and Object and spreads their element. It is represented by three dots(…). We have converted a given number to a string and spread it by using the spread operator. The map() function is applied to each digit of given number to typecast it to number and returned an array of numbers.

#JavaScript Program to convert number to array in ES6 JavaScript
var number = 111369;




let NumberToArr = [...number.toString()].map(Number);
 

 
 
console.log('number to array:\n',NumberToArr) 

Output

number to array:[1,1,1,3,6,9]

Summary

In this post, we have learned multiple ways how to convert numbers to an array in ES6 JavaScript with example.