I want to convert an integer, say 12345, to an array like [1,2,3,4,5].
I have tried the below code, but is there a better way to do this?
var n = 12345;
var arr = n.toString().split('');
for (i = 0; i < arr.length; i++) arr[i] = +arr[i] | 0;
This single line will do the trick:
Array.from(String(12345), Number);
const numToSeparate = 12345;
const arrayOfDigits = Array.from(String(numToSeparate), Number);
console.log(arrayOfDigits); //[1,2,3,4,5]
1- String(numToSeparate) will convert the number 12345 into a string, returning '12345'
2- The Array.from() method creates a new Array instance from an array-like or iterable object, the string '12345' is an iterable object, so it will create an Array from it.
3- In the process of creating this new array, the Array.from() method will first pass any iterable element (ex: '1', '2') to the callback we declare as a second parameter (which is the Number function in this case). This is possible because an String is an "array-like" object.
To make it simpler, we could've declared the callback as:
Array.from(String(numToSeparate), n => Number(n)
4- The Number function will take any string character and will convert it into a number eg: Number('1'); will return 1.
5- These numbers will be added one by one to a new array and finally this array of numbers will be returned.
Summary
The code line Array.from(String(numToSeparate), Number); will convert the number into a string, take each character of that string, convert it into a number and put in a new array. Finally, this new array of numbers will be returned.
I'd go with
var arr = n.toString(10).replace(/\D/g, '0').split('').map(Number);
You can omit the replace if you are sure that n has no decimals.
Number function. This is smart :-)Number function converts a string to a numbervar n = 12345;
var arr = ('' + n).split('').map(function(digit) {return +digit;});
The map function, though, is only supported by recent browsers.
map function is not supported in MSIE8-This method is faster than method with toString transform. Approximately 3-4 times. You can see benchmarks here or on screen below.
const numberToArray = (number) => {
var arr = [];
while(number>0) { arr.push(number%10); number = number/10|0; }
return arr.reverse();
}
I hope I am not too late to the party, but...
If you are code golfing, then do
const n = 9876
const a = [...""+n].map(n=>+n) // 20 chars
console.log(a)
Explanation:
""+ converts n into a string to concatenate it with the "" part... is the spread syntax. It expands the string ""+n into an array+ before n in map just converts back every array element from string to numberThe rest should be clear. Cheers!
You can just use the String() method. It coverts to string but you can use them in term of digits.
But if you want the data as array of string
let val = 1458;
let numbers = String(val);
console.log("First response => ",numbers);
let fromNumberToArray = [...val.toString()].map(Number);
console.log("Second response => ",fromNumberToArray)
This is a very simple way using basic javascript that makes sense:
function toNumArray(num) {
num = num.toString();
let numArr = []
for (let i = 0; i < num.length; i++) {
numArr.push(num[i])
}
return numArr;
}
console.log(toNumArray(911956789874))
don't forget the case where the number is negative
function toArr2(num) {
var myArr = String(Math.abs(num)).split("").map((num)=>{
return Number(num)})
if (num <0) myArr[0]*=-1;
return myArr;
}
console.log(toArr2(123));
console.log(toArr2(-123));
var n = 12345;
var s = String(n).split('').map(Number);
console.log(s);
.map(Number)or.map(parseInt), but that doesn't castNaNto0as your bitwise operation does.|0?.map(Number)is also not supported in IE8.