Jak przekonwertować numer na tablicę w JavaScript za pomocą tablicy.
const n = 123456;
Array.from(n.toString()).map(Number);
// [1, 2, 3, 4, 5, 6]
Blue Badger
const n = 123456;
Array.from(n.toString()).map(Number);
// [1, 2, 3, 4, 5, 6]
const myNumber = 1245;
function numberToArray(number) {
let array = number.toString().split("");//stringify the number, then make each digit an item in an array
return array.map(x => parseInt(x));//convert all the items back into numbers
}
//use the function
var myArray = numberToArray(myNumber);
const numToSeparate = 12345;
const arrayOfDigits = Array.from(String(numToSeparate), Number);
console.log(arrayOfDigits); //[1,2,3,4,5]
const n = 123456;
Array.from(n.toString()).map(Number);
// [1, 2, 3, 4, 5, 6]
const arrayOfDigits = numToSeparate.toString().split("");