Home > front end >  JavaScript: Convert number to comma separated Array then back again
JavaScript: Convert number to comma separated Array then back again

Time:10-17

function indexNumber(number) {
    return Array.from(number.toString()).map(Number)
}

Input = 123
Output = [ 1, 2, 3 ]

I'm wanting to convert it back again. Any help would be appreciated.

CodePudding user response:

'123'.split("").map(e =>Number(e)) [1,2,3].join("")

CodePudding user response:

You just have to join and convert string to number

function digitsToNumber(digits) {
  return  digits.join("")
}

console.log(digitsToNumber([1, 2, 3]))

  • Related